Re: Generics Question
Re: Generics Question
- Subject: Re: Generics Question
- From: Michael de Haan <email@hidden>
- Date: Thu, 24 Sep 2015 11:57:49 -0700
Nice… thank you Marco
> On Jul 26, 2015, at 4:43 PM, Marco S Hyman <email@hidden> wrote:
>
>> func genericFor<T>(s:String) -> T {
>>
>> return T(s)! // error. ’T’ cannot be constructed because it has no accessible initializers
>> }
>
> At compile time there is no way of determining if T has an initializer that
> takes a string. You could do something like this
>
> ----------------
>
> protocol InitFromString {
> init?(_ value: String)
> }
>
> func genericFor<T: InitFromString>(s:String) -> T {
>
> return T(s)!
> }
>
> ----------------
>
> Then add extensions to the various types you want to convert to make sure
> they conform to the InitFromString protocol.
>
_______________________________________________
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