3 ms·
Yup. 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 wor
by happytoexplain 2y ago
Yup. 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.