2016-09-19 102 views
2

当函数的返回值是另一个函数时,无法获得返回的函数的参数名称。这是否是快捷语言的陷阱?在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) 
} 

谨记townPlantownPlan(lightsToAdd: 3, toLights: 8)会更明智的townPlan(3, 8),对不对?

回答

2

你是对的。从Swift 3发行说明:

参数标签已从Swift函数类型中删除...未应用的对函数或初始化程序的引用不再携带参数标签。

因此,从townPlan主叫makeTownGrand返回的类型,即类型,是(Int,Int) -> Int - 和不携带外部参数标签信息。

有关基本原理的完整讨论,请参阅https://github.com/apple/swift-evolution/blob/545e7bea606f87a7ff4decf656954b0219e037d3/proposals/0111-remove-arg-label-type-significance.md

相关问题