Partial application of protocol method is not allowed
Asked Answered
P

2

8

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

protocol Test {
    func someFunc() -> String
    var someClosure: () -> Int { get }
}

class A: Test {
    func someFunc() -> String {
        return "A String"
    }

    var someClosure: () -> Int {
        return {
            return 2
        }
    }
}

class B {
    let a: Test
    let aString: () -> String
    let aInt: () -> Int

    init(a: Test){
        self.a = a

        aString = a.someFunc // Error: Partial application of protocol method is not allowed
        aInt = a.someClosure // Works fine
    }
}

UPDATE

Also here is my weird segmentation fault collection https://gist.github.com/aleksgapp/795a2d428008bdfa4823

Do not hesitate to comment if you have some thoughts about any.

Pennypennyaliner answered 5/9, 2014 at 11:21 Comment(1)
Since Xcode7 beta 2 this is workingSheikh
M
7

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.

Misdirection answered 22/12, 2014 at 18:5 Comment(2)
Thanks for the great explanation. I think it's sad though.Toussaint
With Xcode7 beta 2 this has been changed to work now: "Non-mutating methods of structs, enums, and protocols may now be partially applied to their “self” parameter."Sheikh
S
3

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

aString = { return a.someFunc() }
Skippy answered 9/9, 2014 at 14:4 Comment(1)
Worked for me! Awesome.Loosestrife

© 2022 - 2024 — McMap. All rights reserved.