Swift: Initializing instance of a generic object in a generic class will only initialize its base class

Originator:ggurantz
Number:rdar://19136247 Date Originated:03-Dec-2014 04:21 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Version 6.1.1 (6A2006)
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:
I have a generic class with a restriction <T: BaseClass>. If I initialize instances of T within my generic class, such as by T(), it will return instances of BaseClass even if I initialize my generic class with a subclass of BaseClass

Steps to Reproduce:
Copy this into a playground or xcode project and run:

---------------------------------

class BaseClass { 
}

class SubClass: BaseClass {
    var value: String { return "yay" }
}

class GenericClass<T: BaseClass> {
    func createObject() -> T {
        return T()
    }
}

let generic = GenericClass<SubClass>()
let object: SubClass = generic.createObject()
let value = object.value // EXC_BAD_ACCESS


Expected Results:
Object should be an instance of "SubClass". Value should equal "yay"

Actual Results:
Object is an instance of "BaseClass". Calling "value" on it causes EXC_BAD_ACCESS

Version:
XCode 6.1.1 and 6.1, OSX Yosemite 10.10

Notes:


Configuration:
This still occurs even if I create custom initializers in BaseClass.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!