Nice… thank you Marco
> On Jul 26, 2015, at 4:43 PM, Marco S Hyman wrote:
>
>> func genericFor(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 initi
> . That’s why I was asking about a more realistic example of the problem
> you’re trying to solve.
Point taken.
This was just a “trivial” … well it seems not that trivial…example to deepen my
understanding of Generics. So, I thank you, and will repost with a more
specific example if and whe
On Jul 26, 2015, at 16:30 , Michael de Haan wrote:
>
> func genericFor(s:String) -> T {
>
>return T(s)! // error. ’T’ cannot be constructed because it has no
> accessible initializers
> }
This *generic* definition asks for an invocation of ‘init (_ s: String)’ *for
any type that satis
> func genericFor(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 Ini
>
>
> 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 ge
On Jul 26, 2015, at 15:49 , Michael de Haan wrote:
>
> func genericTFor(s:String) -> T {
>
>return T(s)!
>
> }
>
>
>
> let intFromStr = intFor("9")
> let intFromStrB:Double = genericTFor("9")
>
>
> in other words, write a generic function that will return an Int, Doub
Oops… I should have called it genericTfor etc as in:
>>>
func intFor(s:String) -> Int {
return Int(s)!
}
func genericTFor(s:String) -> T {
return T(s)!
}
let intFromStr = intFor("9")
let intFromStrB:Double = genericTFor("9")
in oth
I’m writing a coreData helper which will rely on Generics.
Could I get some input?
I have synthesized the problem down to this somewhat nonsensical code.
From Playground:
func intFor(s:String) -> Int {
return Int(s)!
}
func genericIntFor(s:String) -> T {
r