Static when calling closure expressions


(Marko Havu) #1

The following fails, because Silver requires test_passes and test_fails to be static:

class MyTestCase: XCTestCase {
    static var allTests = {
        return [
            ("test_passes", test_passes),  // E: "test_passes" is not static
            ("test_fails", test_fails),  // E: "test_fails" is not static
        ]
    }()

    func test_passes() {
        XCTAssert(true)
    }

    func test_fails() {
        XCTAssert(false)
    }
}

(marc hoffman) #2

Hmm. I don’t see how they could not be required to. a static method/property, allTests cannot access/call instance members of a type.

what should this compile to? what’t the (implied) return type of allTests, an array of tuples of a string and what exactly?


(Marko Havu) #3

Yeah, I was thinking about this, too. I couldn’t find the right part of the language spec to see what is supposed to happen here, but this is how Apple writes their test cases. With their compiler allTests is of type [(String, (MyTestCase) -> () -> ())].


(marc hoffman) #4

Ugh. @ck, any thoughts?


(Carlo Kok) #5

Curious, why the double delegate? In any case it seems that Swift allows (MyTestcase) -> () to point to a static method and call it through that.

So basically this:

typealias test = (MyClass) -> ()

class MyClass {
    static var inst: test = TakeThis;
    func TakeThis() {
        
    }
}

should work. I’ll create an issue.


(RemObjects) #6

Thanks, logged as bugs://81986


(RemObjects) #7

bugs://81986 got closed with status fixed.


(Marko Havu) #8

Apple’s compiler is not happy with the above:
Cannot convert value of type '(MyClass) -> () -> ()' to specified type 'test' (aka '(MyClass) -> ()')


(Carlo Kok) #9

hrmm this is quite odd. From the error it seems it returns a delegate that takes an instance of the class and returns delegate. Fairly indirect. Ill investigate further.