|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
[scala] 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 |
|
|
Re: [scala] Disjoint unions in JavaI 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@...>
-- Thanks, -Vlad |
|
|
Re: [scala] Disjoint unions in JavaHi 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/ |
| Free embeddable forum powered by Nabble | Forum Help |