Adding Either type to the Standard Library

It's extremely easy to come up with reasonable type syntax for writing anonymous sums and fairly difficult to come up with reasonable expression/pattern syntax for injecting and projecting values in and out of those types. The most natural syntax by analogy with tuples would be to use integer case labels (.1, .2, etc.), but this problematically clashes with floating-point literals. Many people who want anonymous sums seem to want both injection and projection to be implicit, which is very much not analogous to tuples and would cause quite a few type-checking problems. You could require labels to be written out in the type, although it would make the types a lot less concise. But instead of discussion of these issues, I hear a lot about math, as if the only reason to be wary of the feature is that people are unaware of type algebra, even when people are manifestly not unaware of type algebra.

I'm starting to lean towards providing an Either in the library. Defining a real enum would usually be the better choice for an API, just like defining a real struct is usually better than using a tuple, but it's very convenient in implementations to have a type at hand with the right conformances and so on. The downsides of the type (just two cases, meaningless first/second labels) probably provide the right push towards real enums.

A variadic OneOf seems to have worse usability problems than numeric labels. Would all the cases be named the same thing? With due respect, that feels like abstract box-checking rather than putting any real thought towards design.

8 Likes