Convenience initializer inheritance issue

I simplified my code that was getting the error into the following in order to show what the issue is. The problem seems to happen in subclasses of generics. Copy and past the code into a playground and you shoudl get the same results.




class MyClass {

    convenience init?(succeed:Bool) {
        guard succeed else {return nil}
        self.init()
    }

    init(){}

}
class MySubClass:MyClass {}

class MyGenericClass<T> {

    convenience init?(succeed:T) {
        guard succeed as? Bool ?? false else {return nil}
        self.init()
    }

    init(){}

}
class MyGenericSubClass<T>:MyGenericClass<T> {}
class MyTypedGenericSubClass:MyGenericClass<Bool> {}

MyClass(succeed: true) // Works
MySubClass(succeed: true) // Works
MyGenericClass<Bool>(succeed: true) // Works
MyGenericSubClass<Bool>(succeed: true) // Fails
MyTypedGenericSubClass(succeed: true) // Fails
/*
Gets the following error:

Playground execution failed: error: MyPlayground.playground:59:1: error: '(Bool) -> MyGenericSubClass<Bool>' is not convertible to '(Bool) -> MyGenericSubClass<Bool>?'
MyGenericSubClass<Bool>(succeed: true) // Fails
^~~~~~~~~~~~~~~~~~~~~~~
error:MyPlayground.playground:60:1: error: '(Bool) -> MyTypedGenericSubClass' is not convertible to '(Bool) -> MyTypedGenericSubClass?'
MyTypedGenericSubClass(succeed: true) // Fails
^~~~~~~~~~~~~~~~~~~~~~
*/



Is this the intended behavior?

Replies

What version of Xcode are you working with? I put your code into a new command line tool project and it won’t compile (I get compile-time errors on line 29 and 30).

I’m using Xcode 8.2 with a new project created from the macOS > Command Line Tool template.

Please repeat your test in a command line tool target. Clearly something odd is happening here, but I’d like to know whether it’s an artefact of you working in a playground or something else about your environment.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"