3 ms·
The dark secret of Optional in Swift is all the extensions that make it work magic in situations far beyond the sugary ? ?? ! Disappointing, really, when someo
by w10-1 2y ago
The dark secret of Optional in Swift is all the extensions that make it work magic in situations far beyond the sugary ? ?? !
Disappointing, really, when someone finds the purloined letter hiding in plain sight and then... doesn't open it?? :)
- happytoexplain 2y agoYup. I wonder if there is a single place documenting all the ways the compiler uses explicit knowledge of Optional? E.g. this uses no sugar, but it wouldn't work for any other wrapper type: func foo(string: Optional<String>) { . . . } let string: String = "just a string" foo(string: string)
- grumpyprole 2y agoSo Swift does implicit conversions?
- happytoexplain 2y agoNot generally, no. Optional is a special case. It's the only special case I'm aware of - though possibly some of the things they added for SwiftUI might also do this? Like `Binding`? I could be wrong. Point is, it's not a feature available for developers to use with their own types. Edit: 'Binding' uses the PropertyWrapper attribute, which is similar in that it allows treating a wrapper type like the type it wraps. This is a language feature that is available to users. I'd be interested to know if Optional is the only "magical" type.