Pre-SIP: A New Type for Optionals and Error Handling (2)

Instead of having the A <: A? subtyping relationship when A is not nullable, we could make an implicit A <:< A? instance available. Then we could use that to perform casts like List[Int] to List[Int?] explicitly using e. g. liftCo.

Set is actually invariant, so that’s not going to work.

Oops! I should have used List.

I think it’s a bit dangerous to use <:<, though, because as a witness it’s often assumed to correspond to actual subtyping so that asInstanceOf ought to be safe. Even though one is supposed to use the apply method and thus could use it as a conversion, I’ve seen plenty of code where it is purely used as a witness.

And if it’s just a witness, it’s no better than <:. You need a chance to wrap, which means you need at least an inline type-dispatched option to run something.

1 Like

I don’t understand this concern. The problem pointed out by @eugengarkusha is that there’s a subtyping relationship between different levels of nesting of ?, which can cause confusion because you can inadvertently pass an A? where A?? is expected. Using a <:< instance instead solves that problem because now you have to perform the conversion from A? to A?? explicitly. It could be that <:< is intended purely as a witness by the programmer, but the compiler doesn’t know what, so an explicit conversion is required anyway.

1 Like

On second thought, it’s easily possible to turn A <:< A? into A? <:< A??, and that’s not a good idea. I think we just need to get rid of the whole A <: A? thing, covariance of ? is more important.

T ? E could be sugar for into[Maybe[T, E]] so then pass T to T? with conversion?

NamedTuple <=> Tuple conversion had some manual special casing which could be repeated also