Partial application of protocol method is not allowed

蓝咒 提交于 2019-11-30 19:46:41

UPDATE (thanks to Laszlo Korte)

From Xcode 7 Beta 2 with Swift 2.0 Release Notes: Non-mutating methods of structs, enums, and protocols may now be partially applied to their “self” parameter.

For example:

let a: Set<Int> = [1, 2] 
let b: [Set<Int>] = [[1], [3]]

b.map(a.union) // [[1, 2], [1, 2, 3]]

ORIGINAL ANSWER (correct for Xcode 6 with Swift 1.2)

Protocol can be adopted by class, structure or enumeration. In last two cases partial application of structure or enumeration method is not allowed and you get "Partial application of protocol method is not allowed" because a: Test can be structure or enumeration.

Partially applied method or function is in other words curried method or function. So when you write a.someFunc you try to partially apply this method, i.e. get reference to closure that implicitly holds reference to a. But structures and enumerations are not reference types, they are value types and there are no reference to a.

So, I can't speak to why it behaves like this, but I did find a workaround. Try this:

aString = { return a.someFunc() }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!