On 06/09/2015 21:50, Charles Srstka wrote:
On Sep 6, 2015, at 3:19 PM, Quincey Morris <quinceymor...@rivergatesoftware.com <mailto:quinceymor...@rivergatesoftware.com>> wrote:

(But merely defining a protocol for each of your subclasses is not an improvement here.)

It does, however, seem to avoid the crash:

class ObjectBase<T> {
required init() {}
}

protocol MyProtocol {
func foo()
func bar()
func baz()
}

class MyObject: ObjectBase<MyProtocol>, MyProtocol {
func foo() { print("foo") }
func bar() { print("bar") }
func baz() { print("baz") }
required init() {}
}

let obj = MyObject()

compiles and runs without errors.


Unfortunately, that doesn't help here since MyObject's methods need to return instances of the parameterized type. That's the bit that's causing the crashes.

e.g. This test case compiles successfully, but crashes at runtime on instantiating YourObject:

    class MyObject<T> {
        required init() {}
        func foo() -> T { return self.dynamicType.init() as! T }
    }

    class YourObject: MyObject<YourObject> {
        required init() {}
    }

    let obj = YourObject()


(It's not a realistic example as I need to return types other than Self, but if it had worked I might've used it as a starting point for figuring out a solution.)


This expresses my requirements, but won't compile because the compiler doesn't know what initializers T implements without more information:

    class MyObject<T> {
        required init() {}
        func foo() -> T { T() }
    }

    class YourObject: MyObject<YourObject> {
        required init() {}
    }

    let obj = YourObject()


But after adding a protocol to supply this information, SourceKit and swiftc crash upon reading the code:


    protocol Object {
        init()
    }

    class MyObject<T: Object> {
        required init() {}
        func foo() -> T { T() }
    }

    class YourObject: MyObject<YourObject>, Object {
        required init() {}
    }

    let obj = YourObject()

As suggested, I'm going to write these up as a radar ticket.


Thanks,

has

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to