Partial application of protocol method is not allowed

后端 未结 2 1127
情深已故
情深已故 2021-01-05 01:36

Can someone explain this error and why this works with closure?

If you change \'Test\' to \'A\' inside \'B\' class everything works in both cases.

beta 7

相关标签:
2条回答
  • 2021-01-05 02:22

    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.

    0 讨论(0)
  • 2021-01-05 02:37

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

    aString = { return a.someFunc() }
    
    0 讨论(0)
提交回复
热议问题