Kannan Goundan | 21 Jun 06:02
Favicon

Disjoint unions in Java


(Off-topic, but hopefully people on this mailing list have some 
experience/interest in this sort of thing.  I apologize if this isn't 
appropriate.)

I'm looking for a technique to simulate disjoint unions in Java.  The 
technique should produce something that is easy to use from within Java 
(i.e. I'm not just looking for a way to encode disjoint unions on the 
JVM).

Right now, I'm using an abstract base class with a concrete subclass for 
each option.

One issue is that matching an option requires a verbose "instanceof" 
check along with a type cast.  It would be nice to be able to use a 
"switch" (is there a way to encode disjoint unions using Java enums?).

Another issue is that I want to allow union types to be extended with new 
options.  For example:

  type Bool = True | False
  type TriState extends Bool = Maybe
     // TriState can be True, False, or Maybe

Here, Bool is a subtype of TriState.  TriState describes a larger set of 
values than Bool.

I'd appreciate any relevant tips or references.

Thanks.
- Kannan


Gmane