generics - Initializers as a way of converting to new types in Swift -
i have next type called maybe
:
enum maybe<t>:{ case nil case something(t) init(){ self = .nothing } init(_ something: t){ self = .something(something) } }
i expecting code phone call sec initializer, convert int
maybe<nsdate>
:
var c : maybe<nsdate> = nsdate()
no so. how can create above code work (as builtin optional
does)?
you need phone call initializer:
var c = maybe(nsdate()) // type of c inferred maybe<nsdate>
swift's optional
type relies on compiler magic allow direct assignment some<t>
case without explicitly calling initializer.
generics swift optional initializer
No comments:
Post a Comment