加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift中,没有办法获得返回函数的参数名称?

发布时间:2020-12-14 05:46:00 所属栏目:百科 来源:网络整理
导读:当函数的返回值是另一个函数时,没有办法获得返回函数的参数名称.这是一个快速语言的陷阱吗? 例如: func makeTownGrand(budget:Int,condition: (Int)-Bool) - ((Int,Int)-Int)?{ guard condition(budget) else { return nil; } func buildRoads(lightsToAdd
当函数的返回值是另一个函数时,没有办法获得返回函数的参数名称.这是一个快速语言的陷阱吗?

例如:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读