Xcode 为什么swift 3在闭包参数中需要``?

Xcode 为什么swift 3在闭包参数中需要``?,xcode,swift3,Xcode,Swift3,我是swift 3的新手。我有现有的代码,想把它转换成swift 3。我只是好奇为什么xcode要求我在参数名之前插入。 func anotherClosure(age: Int, name: String, handler: (_ name: String, _ age: Int) -> ()) { handler(name, age) } 我正在网上搜索,但找不到答案。如果您有更好的方法创建具有多个值的闭包,并将其传递给处理程序,请在下面进行评论 感谢在Swi

我是swift 3的新手。我有现有的代码,想把它转换成swift 3。我只是好奇为什么xcode要求我在参数名之前插入

func anotherClosure(age: Int, name: String, handler: (_ name: String, _ age: Int) -> ()) {
        handler(name, age)
    }
我正在网上搜索,但找不到答案。如果您有更好的方法创建具有多个值的闭包,并将其传递给处理程序,请在下面进行评论


感谢在Swift 3之前,就类型系统而言,参数名称是类型的一部分。然而,强制使关键字名称正确匹配将使使用闭包成为一场噩梦。因此,类型系统忽略了它们,这就引出了一个问题:为什么它们在名字中是类型的一部分

import CoreFoundation

func applyAndPrint(closure: (a: Double, b: Double) -> Double, _ a: Double, _ b: Double) {
    print(a, b, closure(a: a, b: b))
}

//All these have different types, because of their different keyword parameter names.
let adder: (augend: Double, addend: Double) -> Double = { $0 + $1 }
let subtractor: (minuend: Double, subtrahend: Double) -> Double = { $0 - $1 }
let multiplier: (multiplicand: Double, multiplier: Double) -> Double = { $0 * $1 }
let divider: (dividend: Double, divisor: Double) -> Double = { $0 / $1 }
let exponentiator: (base: Double, exponent: Double) -> Double = { pow($0, $1) }
let rooter: (degree: Double, Radicand: Double) -> Double = { pow($1, 1/$0) }

// Yet the type system ignores that, and all these are valid:
applyAndPrint(adder, 2, 3)
applyAndPrint(subtractor, 2, 3)
applyAndPrint(multiplier, 2, 3)
applyAndPrint(divider, 2, 3)
applyAndPrint(exponentiator, 2, 3)
applyAndPrint(rooter, 2, 3)

在Swift 3之前,就类型系统而言,参数名称是类型的一部分。然而,强制使关键字名称正确匹配将使使用闭包成为一场噩梦。因此,类型系统忽略了它们,这就引出了一个问题:为什么它们在名字中是类型的一部分

import CoreFoundation

func applyAndPrint(closure: (a: Double, b: Double) -> Double, _ a: Double, _ b: Double) {
    print(a, b, closure(a: a, b: b))
}

//All these have different types, because of their different keyword parameter names.
let adder: (augend: Double, addend: Double) -> Double = { $0 + $1 }
let subtractor: (minuend: Double, subtrahend: Double) -> Double = { $0 - $1 }
let multiplier: (multiplicand: Double, multiplier: Double) -> Double = { $0 * $1 }
let divider: (dividend: Double, divisor: Double) -> Double = { $0 / $1 }
let exponentiator: (base: Double, exponent: Double) -> Double = { pow($0, $1) }
let rooter: (degree: Double, Radicand: Double) -> Double = { pow($1, 1/$0) }

// Yet the type system ignores that, and all these are valid:
applyAndPrint(adder, 2, 3)
applyAndPrint(subtractor, 2, 3)
applyAndPrint(multiplier, 2, 3)
applyAndPrint(divider, 2, 3)
applyAndPrint(exponentiator, 2, 3)
applyAndPrint(rooter, 2, 3)