Re: Generics Question
Re: Generics Question
- Subject: Re: Generics Question
- From: Michael de Haan <email@hidden>
- Date: Sun, 26 Jul 2015 16:30:20 -0700
>
>
> What you really need to do depends on what you’re really trying to achieve.
Sorry, it’s been a long day :-)
I am really trying to understand this. So….. given this… and I think this will make sense now,
*********
func intFor(s:String) -> Int {
return Int(s)!
}
func genericFor<T>(s:String) -> T {
return T(s)! // error. ’T’ cannot be constructed because it has no accessible initializers
}
let intFromString = intFor("8") // —>>> 1
let doubleFromStr:Double = genericFor("9") // 2
let anotherIntFromStr:Int = genericFor("7")
***********
(1) intFromString is straight forward. it returns an integer, 8
(2) I create a constant, “doubleFromStr” and type it as a double, then call the “generic” "genericFor<T>(s:String) -> T”
thinking that the return
"T(s)!”
could do what was done similarly in the call to “intFromString”.
But, as you can see, I get the error shown. So, wondering if what I am trying to do is possible, or just a misreading of “Generics"
Sorry for all the confusion.
_______________________________________________
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