Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial application of protocol method is not allowed

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.

like image 340
Aleksey G. Avatar asked Sep 05 '14 11:09

Aleksey G.


2 Answers

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.

like image 54
mixel Avatar answered Nov 10 '22 08:11

mixel


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

aString = { return a.someFunc() }
like image 26
Spencer Avatar answered Nov 10 '22 08:11

Spencer