Currently, there are the following ways to represent Enums.
A = Class()
B = Class {x = Int; y = Int}
E = Class A or B
x = E.new A.new()
_ = match x:
a: A -> ...
b: B -> ...
However, this has the following drawbacks:
Have to define a class for each option. Otherwise, it cannot be named
Have to pass the constructor twice
Poor pattern matching
Redundant when defining a simple set of singletons
Cannot implement Ord automatically
So I am thinking to add a built-in function called Enum.
E = Enum {
# All constructors must be public
.A = ... # a singleton can be defined with `...`
.B = (Int, Int)
.C = {x = Int}
}
# Typeof(x) != Nat
x: E = E.A
E.B: (Int, Int) -> E
_ = match x:
E.A -> ...
E.B(1, 2) -> ... # constant pattern
E.B(a, 2) -> ...
E.B(a, b) -> ...
E.C{x} -> ...
Currently, there are the following ways to represent Enums.
However, this has the following drawbacks:
Ord
automaticallySo I am thinking to add a built-in function called
Enum
.