带有 Void 的通用类型别名在 Swift 4 中给出错误缺少参数

Generic typealias with Void gives error missing argument in Swift 4

当我尝试迁移到 Swift 4 时,如果您将 Void 作为输入,我的闭包通用类型别名会发生错误。这在 Swift 3 中运行良好,但现在效果不佳,有人知道为什么,这是错误还是功能?

有一种变通方法是拒绝另一个显式处理这种情况的闭包。但是最好不必使用此解决方法并理解为什么 Swift 4.

中会出现此错误
typealias Handler<T> = (T) -> Void

func foo(completion: Handler<String>) {
    completion("fooing")
}

// This worked in Swift 3, but not in Swift 4
func bar(completion: Handler<Void>) {
    completion() // Missing argument for parameter #1 in call
}

这似乎不是错误,而是 Swift 现在的工作方式。您不能再省略 Void 类型的关联值。

我发现了两个可行的解决方法:

第一个只是显式地传递值。

func bar(completion: Handler<Void>) {
    completion(())
}

第二种解决方案是为无效情况声明另一个类型别名。

typealias VoidHandler = () -> Void

func barbar(completion: VoidHandler) {
    completion()
}