在扩展了Bool
之后,我感到非常兴奋,我想在Swift中扩展闭包(我们在Smalltalk中做到这一点时没有任何问题,为什么不呢?)。
这是我的播放器:
typealias NiladicClosure = () -> ()
extension NiladicClosure {
var theAnswerToLife:Int {
return 42
}
}
let block:NiladicClosure = {}
block.theAnswerToLife
它无法工作,显示NiladicClosure does not have a member named 'theAnswerToLife'
。查看控制台,我得到了更多的信息:
Playground execution failed: /var/folders/2k/6y8rslzn1m95gjpg534j7v8jzr03tz/T/./lldb/33726/playground119.swift:3:1: error: non-nominal type 'NiladicClosure' cannot be extended
extension NiladicClosure {
^ ~~~~~~~~~~~~~~
什么是非名义类型
?是否有模式或解决方法?
其他类似的问题早在Swift 2之前就存在了,也足够具体,以至于人们为特定扩展提供了解决方法。我想知道Swift闭包是否是一等对象,我是否可以像在Swift中处理其他东西一样添加附加行为。