2016-09-19 42 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

相關問題