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 class. Use the keyword val to define the select instance of the enumeration, like CNY, GBP,… in the example. Assign these specialized names to a special Value, which represents the type of the enumeration within its definition—Value isn’t directly accessible outside of the enumeration.

That sounds good, but there’s a problem. Remember how the name of a singleton refers to an instance. For example, MarkerFactory in the previous example referred to the singleton instance defined using object. With enumerations, however, we do want to refer to the enumeration name as a general reference to any one of the values. For example, take a look at this Money class:

WorkingWithObjects/finance1/finance/currencies/Money.scala
 
package​ finance.currencies
 
 
import​ Currency._
 
 
class​ Money(​val​ amount: ​Int​, ​val​ currency: Currency) {
 
override​ ​def​ toString = s​"$amount $currency"
 
}

In the constructor of Money we want to receive a Currency as a parameter. It would make no sense to treat the word Currency in val currency: Currency as an instance. Instead it should be considered a type. Now you know the reason for the line type Currency = Value—this tells the compiler to treat the word Currency as a type instead of an instance. We see type aliasing from Type Aliasing being applied here.

We can iterate over the enumeration’s values using the values property:

WorkingWithObjects/UseCurrency.scala
 
import​ finance.currencies.Currency
 
 
object​ UseCurrency ​extends​ App {
 
Currency.values.foreach { currency => println(currency) }
 
}

Scala automatically creates meaningful methods like toString that displays appropriate names for the enumerations as we see in the output:

 
CNY
 
GBP
 
INR
 
JPY
 
NOK
 
PLN
 
SEK
 
USD

In Java to create a singleton you create an enum, but in Scala to create an enum you create a singleton; weird, eh?

As you can see, there are significant departures from Java in the way Scala deals with static and singletons. Next we’ll take a look at another departure from Java: packages can not only have classes, but—surprise—functions also.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.21