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

Re: [scala] Disjoint unions in Java

by Tony Morris-4 :: Rate this Message:

Reply to Author | View in Thread

Hi Kannan,
You might be interested in http://functionaljava.org/ though I don't
know of a way to allow extending union types (easily) in Java.
However, you can use Java enums in switch statements so you could write:

enum TriState { True, False, Maybe }

then switch(ts) {
    case True: ...
    case False: ...
    case Maybe: ...
}

Kannan Goundan wrote:

> (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
>
>
>  

--
Tony Morris
http://tmorris.net/


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