« Return to Thread: [scala] Disjoint unions in Java

Re: [scala] Disjoint unions in Java

by Vlad Patryshev :: Rate this Message:

Reply to Author | View in Thread

I did it for my categorical code in Java.

The idea is to store pairs (x, i), and to define appropriate injection functions.

2009/6/20 Kannan Goundan <kannan@...>

(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




--
Thanks,
-Vlad

 « Return to Thread: [scala] Disjoint unions in Java