public interface Value
Modifier and Type | Method and Description |
---|---|
int |
_c()
The number of the frege constructor that was applied to make this value.
|
int _c()
The number of the frege constructor that was applied to make this value.
The frege compiler compiles algebraic data types to interfaces that
extend Value
and every variant of the type to a class that
implements it's types interface. Here is the frege Maybe type for an example:
data Maybe a = Nothing | Just a
The corresponding java code would be:
public class Prelude { ... public static interface TMaybe<Ta> extends Value, Lazy<TMaybe<Ta>> { public Nothing<Ta> nothing(); // get the Nothing variant public Just<Ta> just(); // get the Just variant public static final class Nothing<Ta> extends Constant implements TMaybe<Ta>, Lazy<TMaybe<Ta>> { public final int _c() { return 0; } // this is a Nothing public final Nothing<Ta> nothing() { return this; } // return me public final Just<Ta> just() { return null; } // no, I am not Just .... } public static final class Just<Ta> extends Tuple1 implements TMaybe<Ta>, Lazy<TMaybe<Ta>> { public final int _c() { return 1; } // this is a Just public final Nothing<Ta> nothing() { return null; } // wrong variant public final Just<Ta> just() { return this; } // return me .... } } ... }
To find out if a Maybe is actually Just or Nothing one can now switch on the return value of _c() or call nothing() and just() in turn to see which one returns a non null value.
The whole scheme is devised so as to avoid type casting and instanceof.
Note that from javas point of view, the variant types are totally unrelated, it is just that they happen to implement the same interface.
Because product types have only one constructor, this will return 0 for every value of a product type.