Enum with when exhaustive check
📖 Scenario: You are building a simple traffic light controller. The traffic light can be RED, YELLOW, or GREEN.You want to write code that reacts to each light color and prints the correct action for drivers.
🎯 Goal: Create an enum class for the traffic light colors, then use a when expression with exhaustive checking to print the correct action for each color.
📋 What You'll Learn
Create an enum class called
TrafficLight with values RED, YELLOW, and GREENCreate a variable called
currentLight and set it to TrafficLight.REDUse a
when expression with variable currentLight to print the correct action for each colorEnsure the
when expression is exhaustive (no else branch)Print the action string exactly as specified
💡 Why This Matters
🌍 Real World
Traffic lights are common in real life and controlling their behavior in software helps understand state management.
💼 Career
Enums and exhaustive when expressions are widely used in Kotlin development for clear and safe handling of fixed sets of options.
Progress0 / 4 steps