Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何将泛型类型限制为闭包?像这样:
struct Closure<T where T:closure> { var closure:T init(_ c:T) { closure = c } }
我认为你不能——相反,使用通用占位符来约束闭包的输入和返回参数,这相当于同一件事:
struct Closure<T,U> { var closure: T->U init(_ c: T->U) { closure = c } } let c = Closure { $0 % 2 == 0 } // c will be a Closure<Int,Bool>