public interface FV extends Lazy<FV>
Modifier and Type | Method and Description |
---|---|
int |
constructor()
The number of the frege constructor that was applied to make this value.
|
int constructor()
The number of the frege constructor that was applied to make this value.
The frege compiler compiles algebraic data types to interfaces that
extend FV
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 extends FV { public Nothing nothing(); // get the Nothing variant public Just just(); // get the Just variant public static final class Nothing extends Val implements TMaybe { public final int _c() { return 0; } // this is a Nothing public final Nothing nothing() { return this; } // return me public final Just just() { return null; } // no, I am not Just .... } public static final class Just extends Prod1 implements TMaybe { 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.