O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Enumerations

In Scala you can use Java enums. Alternately, you can create enumerations in Scala as well.

To create an enumeration in Scala you will start by creating an object—much like the syntax used to create a singleton. However, you can assign multiple named instances—after all, the singleton pattern doesn’t force a single instance; it’s simply a way to control the creation of select instances.

Let’s create an enumeration to represent various currencies:

WorkingWithObjects/finance1/finance/currencies/Currency.scala
 
package​ finance.currencies
 
 
object​ Currency ​extends​ Enumeration {
 
type​ Currency = Value
 
val​ CNY, GBP, INR, JPY, NOK, PLN, SEK, USD = Value
 
}

An enumeration is an object that extends the Enumeration ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required