For Swift, is Double? the same as Optional<Double>? -
var title: double? = nil var title2 = optional<double>.none the 2 things above seem both behave optional doubles. when hold option , click on title , title2, shows have different types. 1 double? , other optional<double>. i'm wondering if theres difference between two. , if aren't different, why have 2 of them? optional objective c thing got transferred on swift or something?
there no difference, , there nothing special double here.
for any type t, t? (compiler built-in) shortcut optional<t>. so
var value: t? var value: optional<t> are equivalent. optional conforms nilliteralconvertible protocol, i.e. value can instantiated literal nil, and
var value: t? = nil var value: t? = .none var value: t? = optional.none var value: t? = optional<t>.none var value: t? = t?.none are equivalent. in first 3 statements, type optional<t> of value on right-hand side inferred type annotation on left-hand side. last 2 statements can shortened to
var value = optional<t>.none var value = t?.none because type inferred automatically right-hand side.
finally, since optionals implicitly initialized .none, all of above statements equivalent.
Comments
Post a Comment