How does covariance work for Optional
s in Swift?
Say I write the following code:
var nativeOptionalView: Optional
let button =
While I agree that there is probably some "compiler magic" going on, this can be accomplished in your custom implementation by casting the button to a UIView, e.g.
var myOptionalButton = MyOptional.Some(button as UIView)
or
var myOptionalButton: MyOptional<UIView> = .Some(button)
It doesn't. Swift does not support custom covariant generics for now.
The Swift type checker is per expression, not global (as in Haskell). This task is handled by the Semantic Analysis in lib/Sema. The constraint system then tries to match the types and special cases of covariance are then handled for collections, and optionals.
This was a language design decision. You should be able to do everything you need with the built-in collection types and optionals. If you aren't you should probably open a radar.