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

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

问题描述

当一个函数的返回值是另一个函数时,没有办法得到返回函数的参数名.这是swift语言的陷阱吗?

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?

例如:

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) 会更明智> 对吧?

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

推荐答案

你说得对.来自 Swift 3 发行说明:

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

参数标签已从 Swift 函数类型中删除...未应用的函数或初始值设定项的引用不再带有参数标签.

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 — 不携带外部参数标签信息.

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

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

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天全站免登陆