Re: Swift generics, circular type declarations, and segfaults, oh my!
Re: Swift generics, circular type declarations, and segfaults, oh my!
- Subject: Re: Swift generics, circular type declarations, and segfaults, oh my!
- From: has <email@hidden>
- Date: Tue, 08 Sep 2015 10:18:16 +0100
On 06/09/2015 21:50, Charles Srstka wrote:
On Sep 6, 2015, at 3:19 PM, Quincey Morris
<email@hidden
<mailto:email@hidden>> 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 (email@hidden)
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:
This email sent to email@hidden
References: | |
| >Swift generics, circular type declarations, and segfaults, oh my! (From: has <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: Fritz Anderson <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: has <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: Quincey Morris <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: has <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: Quincey Morris <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: Charles Srstka <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: has <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: Quincey Morris <email@hidden>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: Charles Srstka <email@hidden>) |