From the course: Introduction to Scala

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Enums

Enums

- [Instructor] Let's talk about enumerations. So enums are amazing for modeling a finite set of values. So an example I'm going to give is a traffic light and you can see here that we're using the enum keyword and a traffic light can be either red, amber, or green. Great so we've written our first enum and this is really helpful. So I am going to give an example of using an enum in a if statement. So before I implement this I'm going to need to import the traffic light enum. So I'm going to do .star to import it. And now I can write if the light is equal to red, then, you know, stop. Else if the light is equal to amber, then get ready. Otherwise the light must be green. So go, okay. So you can see here the nice syntax we can just write red amber because we've got that import on line nine, but it gets even better than this. So I'm going to write this function out again but this time I'm going to use pattern matching.…

Contents