I’ve got a function like this:
我有这样的功能:
func next(step: Int = 1){
//....
}
And now I would like to define a type alias so that I can easily pass around this function.
现在我想定义一个类型别名,以便我可以轻松地传递这个函数。
However I am not able to find a way to declare that the first argument has a default value. I tried things like this
但是我无法找到声明第一个参数具有默认值的方法。我尝试过这样的事情
typealias ActionNext = (step: Int = default) -> ()
var nextFunc: ActionNext = next
but they all give me a error message like
但他们都给我一个错误信息
Default argument not permitted in a tuple type
元组类型中不允许使用默认参数
Is there any way to define a type for this kind of function?
有没有办法为这种函数定义一个类型?
2 个解决方案
#1
7
No, that is not possible
不,那是不可能的
Explanation:
说明:
A type alias declaration introduces a named alias of an existing type into your program.
类型别名声明将现有类型的命名别名引入程序。
(step: Int = 1) -> ()
is not a proper type. A type is for example (step: Int) -> ()
, a default value is not allowed there.
(步骤:Int = 1) - >()不是合适的类型。例如,类型(步骤:Int) - >(),那里不允许默认值。
If you write
如果你写
typealias ActionNext = (Int) -> ()
var nextFunc: ActionNext = next
It works. Or even when you write (step: Int) -> ()
有用。或者甚至在你写的时候(步骤:Int) - >()
But I assume what you want to achieve is being able to call nextFunc()
omitting the parameter and using its default value. That is not possible. To understand why, you can follow the Type Alias Declaration
grammar - in the type
you can not specify default values.
但我假设你想要实现的是能够调用nextFunc()省略参数并使用其默认值。这是不可能的。要了解原因,您可以按照类型别名声明语法 - 在您无法指定默认值的类型中。
#2
-2
The problem is exactly what it says. You cannot provide a default argument in a type alias.
问题正是它所说的。您不能在类型别名中提供默认参数。
it must be like this:
它一定是这样的:
typealias ActionNext = (step: Int) -> ()
Your expectation to be able to set a default value is futile and doesn't make sense. (You showed disappointment with Swift in the comments, but fortunately Swift is doing just fine regarding this.)
您期望能够设置默认值是徒劳的,没有意义。 (你在评论中对Swift表示失望,但幸运的是Swift在这方面做得很好。)
First thing first, the action is a closure, which itself is also a type. A type itself CANNOT have some kind of value. Type is never a concrete value, it is a dimensionless trait of a variable, it's qualitative description.
首先,动作是一个闭包,它本身也是一种类型。类型本身不具有某种价值。类型永远不是具体的值,它是变量的无量纲特征,它是定性描述。
You need to properly understand the relationship between the three terms: Variable - type - value
您需要正确理解三个术语之间的关系:变量 - 类型 - 值
A variable is an entity. It has three things - a name, a Type, and Value.
变量是一个实体。它有三个东西 - 名称,类型和价值。
The value is a realisation of the type trait.
该值是类型特征的实现。
EXAMPLE
例
you could have a variable called myDayOfRest that would be of type DayOfWeek
and can have values of SU MO TU WE TH FR SA
.
你可以有一个名为myDayOfRest的变量,其类型为DayOfWeek,并且可以具有SU MO TU WE TH FR SA的值。
Notice this -> let myDayOfRest be of type DayOfWeek and have a value of SA.
请注意这一点 - >让myDayOfRest为DayOfWeek类型,其值为SA。
in Swift it would be
在斯威夫特,它会
let myDayOfRest : DayOfWeek = SA
Now when you create a type alias
for the DayOfWeek type.. basically you are just giving an alternative name to the TYPE. Never to the concrete variable. Always to the TYPE. But it's not the TYPE that has some value, it is variables who have values.
现在,当您为DayOfWeek类型创建类型别名时,基本上您只是为TYPE提供了一个替代名称。永远不要去具体变量。一直到TYPE。但是,TYPE并不具有某些价值,而是具有价值的变量。
Hopefully now you can see why expecting to be able to provide a default value in a type alias does't make any sense.
希望现在你可以看到为什么期望能够在类型别名中提供默认值没有任何意义。
#1
7
No, that is not possible
不,那是不可能的
Explanation:
说明:
A type alias declaration introduces a named alias of an existing type into your program.
类型别名声明将现有类型的命名别名引入程序。
(step: Int = 1) -> ()
is not a proper type. A type is for example (step: Int) -> ()
, a default value is not allowed there.
(步骤:Int = 1) - >()不是合适的类型。例如,类型(步骤:Int) - >(),那里不允许默认值。
If you write
如果你写
typealias ActionNext = (Int) -> ()
var nextFunc: ActionNext = next
It works. Or even when you write (step: Int) -> ()
有用。或者甚至在你写的时候(步骤:Int) - >()
But I assume what you want to achieve is being able to call nextFunc()
omitting the parameter and using its default value. That is not possible. To understand why, you can follow the Type Alias Declaration
grammar - in the type
you can not specify default values.
但我假设你想要实现的是能够调用nextFunc()省略参数并使用其默认值。这是不可能的。要了解原因,您可以按照类型别名声明语法 - 在您无法指定默认值的类型中。
#2
-2
The problem is exactly what it says. You cannot provide a default argument in a type alias.
问题正是它所说的。您不能在类型别名中提供默认参数。
it must be like this:
它一定是这样的:
typealias ActionNext = (step: Int) -> ()
Your expectation to be able to set a default value is futile and doesn't make sense. (You showed disappointment with Swift in the comments, but fortunately Swift is doing just fine regarding this.)
您期望能够设置默认值是徒劳的,没有意义。 (你在评论中对Swift表示失望,但幸运的是Swift在这方面做得很好。)
First thing first, the action is a closure, which itself is also a type. A type itself CANNOT have some kind of value. Type is never a concrete value, it is a dimensionless trait of a variable, it's qualitative description.
首先,动作是一个闭包,它本身也是一种类型。类型本身不具有某种价值。类型永远不是具体的值,它是变量的无量纲特征,它是定性描述。
You need to properly understand the relationship between the three terms: Variable - type - value
您需要正确理解三个术语之间的关系:变量 - 类型 - 值
A variable is an entity. It has three things - a name, a Type, and Value.
变量是一个实体。它有三个东西 - 名称,类型和价值。
The value is a realisation of the type trait.
该值是类型特征的实现。
EXAMPLE
例
you could have a variable called myDayOfRest that would be of type DayOfWeek
and can have values of SU MO TU WE TH FR SA
.
你可以有一个名为myDayOfRest的变量,其类型为DayOfWeek,并且可以具有SU MO TU WE TH FR SA的值。
Notice this -> let myDayOfRest be of type DayOfWeek and have a value of SA.
请注意这一点 - >让myDayOfRest为DayOfWeek类型,其值为SA。
in Swift it would be
在斯威夫特,它会
let myDayOfRest : DayOfWeek = SA
Now when you create a type alias
for the DayOfWeek type.. basically you are just giving an alternative name to the TYPE. Never to the concrete variable. Always to the TYPE. But it's not the TYPE that has some value, it is variables who have values.
现在,当您为DayOfWeek类型创建类型别名时,基本上您只是为TYPE提供了一个替代名称。永远不要去具体变量。一直到TYPE。但是,TYPE并不具有某些价值,而是具有价值的变量。
Hopefully now you can see why expecting to be able to provide a default value in a type alias does't make any sense.
希望现在你可以看到为什么期望能够在类型别名中提供默认值没有任何意义。