Values passed as enum constructor arguments can be captured into variables by use of pattern matching.
Assume the following enum:
enum Color {
RGB(r : Int, g : Int, b : Int);
HSV(h : Int, s : Float, v : Float);
}
The red channel value can be captured as follows:
var color = Color.RGB(255, 127, 0);
var red = switch (color) {
// Match the Color.RGB constructor and capture value into `r`
case Color.RGB(r, _, _):
// Return the captured red value
r;
// Catch-all for matching remaining constructors
case _:
// Return -1
-1;
}
Try the example on try.haxe.org.