i'm relatively new swift, , 1 thing has frustrated me inability create single type represents both class , protocol (similar class<protocol> *
in objective-c). there protocol<protocol1, protocol2>
operator can create new protocol type multiple protocols; question is, why not possible class , protocol?
i aware can accomplished using generics (e.g. <t: class t: protocol>
). however, seems kludgy, in situations might want store type in instance variable don't want add generics class would considered implementation detail.
i better understand why not possible in swift. specifically, interested in how class , protocol types implemented (or defined) , why not possible combine them single type result.
what this?
let's declare protocol , class
protocol wearable { } class computer { }
then let's declare 3 classes
class applewatch: computer, wearable { } class mac: computer { } class jeans: wearable { }
and method accepts value must extend computer
, conform wearable
func foo<t:computer t: wearable>(something:t) { }
and let's test them:
foo(applewatch()) // ok foo(mac()) // error @ compile time foo(jeans()) // error @ compile time
is mean?
Comments
Post a Comment