Enums
A traffic light is always exactly one of red, yellow or green. An enum
names those choices, a variable holds one of them, and match picks what to
do for each.
Contents
chip lamp -> light Color
switch Enabled -> light Enabled
Code
enum Light { Red, Yellow, Green }
var state: Light = Light.Red
@right out lamp = match state {
Red => Color(1.0, 0.0, 0.0),
Yellow => Color(1.0, 0.8, 0.0),
Green => Color(0.0, 1.0, 0.0),
}
on Clock(interval = 2.0) {
match state {
Red => { state = Light.Green }
Green => { state = Light.Yellow }
Yellow => { state = Light.Red }
}
}
enum Light { Red, Yellow, Green }makes a new type with three variants. ALightis always one of them.Light.Rednames a variant.var state: Light = Light.Redholds one and starts red.- The first
matchis a value. Each arm,Red => ..., gives the value for that variant, and the arms are separated by commas. Inside the arms the variants are written bare, sincestateis already known to be aLight. - The second
matchis a statement. Each arm is a block of steps, and only the block for the current variant runs.
The light starts red. In what order do the colors come, and how long does each one last?
Run
- Compile the chip and paste it in-game. Place a Point Light and a Switch.
- Wire
lampto the Point Light’s Color, and the Switch’s Enabled to the light’s Enabled. Flip the switch on. - In the Point Light’s settings, turn off Use Brick Color. While it is on, the light keeps its brick’s color and ignores the wire.
- Watch the light go red, green, yellow, red, two seconds each.
Under the Hood
Open the chip. state is one Var gate, labeled state.__disc, holding
a number: 0 for Red, 1 for Yellow, 2 for Green, in the order the
variants are declared. An enum without data is that number, with names
the compiler checks for you.
The value match is three Equal gates, each comparing that number with
one variant’s, and three Selects in a row that pick the color. The
statement match in the handler is a Get Variable, then an Equal and a
Branch per arm, with a Set Variable on each arm’s path.
A match must cover every variant. Leave one out and the compiler names it:
enum Light { Red, Yellow, Green }
var state: Light = Light.Red
@right out lamp = match state {
Red => Color(1.0, 0.0, 0.0),
Green => Color(0.0, 1.0, 0.0),
}
Error: [WS054] non-exhaustive match: `Yellow` is not covered
When you add a variant to an enum later, every match that forgets it
fails to compile, so none are missed.
Modify
- Write the handler with a value
matchinstead:state = match state { Red => Light.Green, Green => Light.Yellow, Yellow => Light.Red }. The chain becomes one Get Variable and one Set Variable, with the choice made by Selects in between. - Add a crossing button. Add
@left in walk: execfrom a Button’s Held, and anon walkhandler with a statementmatchthat turns a green light yellow and leaves red and yellow alone (an arm can be an empty block,{}). - Add
@right out stop = match state { ... }, givingtruefor red and yellow andfalsefor green, and wire it to a Brick Property Changer’s Collision Enabled on a barrier brick.
Make
Make a sky light that runs through dawn, day, dusk and night, ten seconds
each. Give each part of the day its own color and its own Brightness,
both chosen with a match on one enum.
chip lamp -> light Color
chip brightness -> light Brightness
switch Enabled -> light Enabled
Good to Know
- A Point Light’s Use Brick Color overrides a wired Color. Turn it off whenever a circuit sets the color.
- The state is a Var gate, so the light comes back at the same color after the world is saved and loaded.
Next: Payloads.