Enum Specialization

You are special, like the enum types

Each branch or instance of the enum can also be used to represent the specific type. This allows us to write straight forward functions which expect a specific value of the enum. This approach eliminates the over-use of the match expression as a way to decide datatypes inside a function.

enum Presidents = Mandela | Obama | Putin | JunPing

We can write the specialization functions like this.

fn zar(prez: Presidents.Mandela): String => prez + " Was the president of South Africa"

This is how to use them.

fn main(): Unit => {
    val myprez = Presidents.Mandela
    println(zar(myprez)) (* compiles successfully *)
    println(rus(myprez)) (* fails *)
}

Last updated