alanrgan / rust-interpreter

0 stars 0 forks source link

Algebraic Data Types #4

Open alanrgan opened 7 years ago

alanrgan commented 7 years ago
def data Option {
     Some(int);
     Nothing;
}

ADTs can be destructured via the following construction:

if [ADT name]? [ident] -> [(e1, e2, ... , en)] {

}

Example:

let x: Option = Some(3);
if Option.Some? x -> (inner_val) {
    print("found inner val " + inner_val);
}

Pattern matching can be done via the case [ident] for construction:

case x {
    for (Option.Some -> inner_val)  {
          print("found some! " + inner_val);
    },
    for (Option.Nothing) {
          print("nothing here...");
    },
    _ -> {}
}
alanrgan commented 7 years ago

When designing ADTs, internal type representation should be considered, as well as how it will interact with the (future) type-inferencer / unification engine.