在Swift中,没有办法获得返回函数的参数名称? [英] In Swift,there's no way to get the returned function's argument names?

查看:120
本文介绍了在Swift中,没有办法获得返回函数的参数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当函数的返回值是另一个函数时,没有办法获得返回的函数的参数名称。这是一个快捷语言的陷阱吗?

例如: / b>

  func makeTownGrand(budget:Int,condition:(Int) - > Bool) - > ((INT,INT) - >智力)? 
{
监护条件(预算)else {
return nil;
}

func buildRoads(lightsToAdd:Int,toLights:Int) - > Int
{
return toLights + lightsToAdd
}

return buildRoads
}

func evaluateBudget(budget:Int) - > Bool
{
返回预算> 10000


var stopLights = 0

如果让townPlan = makeTownGrand(预算:30000,condition:evaluateBudget)
{
stopLights = townPlan(3,8)
}

请注意镇计划, townPlan(lightsToAdd:3,toLights:8)对 townPlan(3,8) ,对吗?

解决方案

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


参数标签已从Swift函数类型中移除...未应用的函数或初始化函数引用不再携带参数标签。因此, townPlan 的类型,即从调用返回的类型 $ makeTownGrand ,是(Int,Int) - > Int - 并且不带外部参数标签信息。



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


When a function's return value is another function,there's no way to get the returned function's argument names.Is this a pitfall of swift language?

For example:

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)
}

Be mindful of townPlan,townPlan(lightsToAdd: 3, toLights: 8) would be much more sensible to townPlan(3, 8), right?

解决方案

You're correct. From the Swift 3 release notes:

Argument labels have been removed from Swift function types... Unapplied references to functions or initializers no longer carry argument labels.

Thus, the type of townPlan, i.e. the type returned from calling makeTownGrand, is (Int,Int) -> Int — and carries no external argument label information.

For a full discussion of the rationale, see https://github.com/apple/swift-evolution/blob/545e7bea606f87a7ff4decf656954b0219e037d3/proposals/0111-remove-arg-label-type-significance.md

这篇关于在Swift中,没有办法获得返回函数的参数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆