Enum
An enum is used to define a type by enumerating its possible values. Unlike traditional enums, MoonBit enums can have data associated with each enumeration. We call each enumeration an enum constructor.
In this example, we define an enum Color
, which has five enum constructors: Red
, Green
, Blue
, RGB
, and CMYK
.
The Red
, Green
, and Blue
values directly represent the colors they describe, while RGB
and CMYK
have data associated with them.
Values like Red
and RGB(255,255,255)
are both instances of the Color
type. To create an instance more explicitly, you can use Color::Red
, similar to creating an instance of a struct.
We use a bit of pattern matching to distinguish different enum constructors in print_color
. It's a control flow similar to switch-case in C-like languages. Here is a slight difference: you can extract the associated data by giving them a name on the left of =>
, and use them as variables on the right side.
We will explore more powerful features of pattern matching in the next chapter.
enum Color {
Red
Green
Blue
RGB(Int, Int, Int)
CMYK(Int, Int, Int, Int)
}
fn print_color(color : Color) -> Unit {
match color {
Red => println("Red")
Green => println("Green")
Blue => println("Blue")
// Take the three Int values from RGB and print them.
RGB(r, g, b) => println("RGB: \{r}, \{g}, \{b}")
CMYK(c, m, y, k) => println("CMYK: \{c}, \{m}, \{y}, \{k}")
}
}
fn main {
let red = Red
let green = Color::Green
let blue = RGB(0, 0, 255)
let black = CMYK(0, 0, 0, 100)
print_color(red)
print_color(green)
print_color(blue)
print_color(black)
}