Nothing is a bit more explicit and robust. The right side of Left is obviously “nothing” and shouldn’t be accessed. Actually it is not of the type B since this would mean something like “null of type B”, but there is nothing to be typed. As a subtype of all types Nothing can easily be used here.
The advantage of out is that the compiler doesn’t restrict some corner cases in striving for sound typing. You can read more about variance in the docs.
This is going o sound stupid, but I want to make sure I understand this.
Consider the first example, with A = Int and B = String. Then.
1. Left<Int>(42) has type Either<Int, Nothing>
2. Because "Nothing is a subtype of String" therefore Either<Int, Nothing> is a subtype of Either<Int, String>
3. Therefore Left<Int>(42) can be used as an Either<Int, String>
Are all three assertions above correct?
If so, I can move on to understand how “when” still works given this weirdness.
I see few problem with the implementation you propose (the main issue is conceptually considering right to be the success & left to be the error in some places, it would be another type … like Result or Try)
But still an Either type would be nice (so does a Try type…)
on the other hand… I’m afraid people abusing Either (fear the Either<A, Ether<B, Either<C,D>>>)… but bad developpers should not be an excuse not to do things…