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 protected]> wrote:
>
>> On Sep 6, 2015, at 3:19 PM, Quincey Morris
>> <[email protected]> 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 protected])
>
> 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/jhull%40gbis.com
>
> This email sent to [email protected]
_______________________________________________
Cocoa-dev mailing list ([email protected])
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 [email protected]