Java is well known for provinding Enum Types since its version 1.5. Powerful, simple to use, they make your code easy to understand, and provide a nice way to implement Singleton Objects, since an Java Application will have only one instance of the each Enum Type. Among some of its advantages Enums variables cannot have any other type other than those defined in the Enum class.
Let us model Gender in a Java Enum Type
Reviewing Java Type-Safe Enum Type
In the above code, there is only to options for any variable of type Gender: MALE or Female. Now what if we want model Enum Types like this in other JVM Languages? Such as Scala, our focus today.
Scala, a Functional and yet Object Oriented language for the Java Platform has no built-in Enumeration type like Java's. Instead, it provides an abstract class Enumeration which can be used :
Let us model Gender in a Java Enum Type
Reviewing Java Type-Safe Enum Type
In the above code, there is only to options for any variable of type Gender: MALE or Female. Now what if we want model Enum Types like this in other JVM Languages? Such as Scala, our focus today.
Scala, a Functional and yet Object Oriented language for the Java Platform has no built-in Enumeration type like Java's. Instead, it provides an abstract class Enumeration which can be used :
Comments
Post a Comment