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:44:15 +0100
On 07/09/2015 02:11, Jonathan Hull wrote:
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
(Think you meant `mySquishyVendor.returnAThing().squishiness`, etc.)
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.
Looking at it in terms of set theory, your returnAThing()->SquishyThing
only returns a subset of all possible Things, so in that respect it does
contradict the protocol's promise to return a value in the set of all
Things. OTOH, if you're asking what interface is available, it doesn't
seem entirely unreasonable to gather all the available type information
and use whatever is most precise in a given situation. It may just be
that the type system syntax as used in your protocol needs to be more
expressive, allowing you to to say 'all results ⊆ all Things'. I'm not a
type theorist though. You might be better off asking over on the Lambda
the Ultimate forums (http://lambda-the-ultimate.org/) or some place like
that.
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>) |
| >Re: Swift generics, circular type declarations, and segfaults, oh my! (From: Jonathan Hull <email@hidden>) |
- Prev by Date:
Re: Swift generics, circular type declarations, and segfaults, oh my!
- Next by Date:
Re: Swift generics, circular type declarations, and segfaults, oh my!
- Previous by thread:
Re: Swift generics, circular type declarations, and segfaults, oh my!
- Next by thread:
Re: Swift generics, circular type declarations, and segfaults, oh my!
- Index(es):