当一个函数的返回值是另一个函数时,无法获取返回函数的参数名称。这是 swift 语言的陷阱吗?
例如:
func makeTownGrand(budget:Int,condition: (Int)->Bool) -> ((Int,Int)->Int)?
{
guard condition(budget) else {
return nil;
}
func buildRoads(lightsToAdd: Int, toLights: Int) -> Int
{
return toLights+lightsToAdd
}
return buildRoads
}
func evaluateBudget(budget:Int) -> Bool
{
return budget > 10000
}
var stopLights = 0
if let townPlan = makeTownGrand(budget: 30000, condition: evaluateBudget)
{
stopLights = townPlan(3, 8)
}
请注意 townPlan
,townPlan(lightsToAdd: 3, toLights: 8)
比 townPlan(3, 8)
更明智>,对吗?
最佳答案
你是对的。来自 Swift 3 发行说明:
Argument labels have been removed from Swift function types... Unapplied references to functions or initializers no longer carry argument labels.
因此,townPlan
的类型,即调用 makeTownGrand
返回的类型,是 (Int,Int) -> Int
— 并且不携带外部参数标签信息。
有关基本原理的完整讨论,请参阅 https://github.com/apple/swift-evolution/blob/545e7bea606f87a7ff4decf656954b0219e037d3/proposals/0111-remove-arg-label-type-significance.md
关于swift - 在 Swift 中,没有办法获取返回函数的参数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39581761/