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: Jonathan Hull <email@hidden>
- Date: Sun, 06 Sep 2015 18:11:18 -0700
I am wondering if the compiler feature which might allow this (and several other things) would be to allow implementors of a protocol to adhere to it with a more restrictive type (either a subclass or an implementer/inheritor of a returned protocol).
For example:
protocol Thing {
var noise:String {get}
}
protocol MyProtocol {
func returnAThing()->Thing
}
class SquishyThing:Thing {
var noise:String {return “Squish”}
var squishiness:Int = 3
}
class PoppingThing:Thing {
var noise:String {return “Pop”}
var poppability:Float = 4.2
}
class SquishyVendor:MyProtocol {
func returnAThing()-> SquishyThing {return SquishyThing()}
}
class PoppingVendor:MyProtocol {
func returnAThing()-> PoppingThing {return PoppingThing()}
}
This would allow you to say things like:
mySquishyVendor.returnAThing.squishiness = 6
myPoppingVendor.returnAThing.poppability = 2.8
instead of:
(mySquishyVendor.returnAThing as! SquishyThing).squishiness = 6
(myPoppingVendor.returnAThing as! PoppingThing).poppability = 2.8
Is there an obvious problem caused by this which I am missing? I can think of 3 or 4 places where it would shrink my code quite a bit.
Thanks,
Jon
> On Sep 6, 2015, at 1:50 PM, Charles Srstka <email@hidden> wrote:
>
>> On Sep 6, 2015, at 3:19 PM, Quincey Morris <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.
>
> Charles
>
> _______________________________________________
>
> 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
_______________________________________________
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>) |