Showing posts with label case-object. Show all posts
Showing posts with label case-object. Show all posts
Sunday, August 23, 2009
Enumerations
Scala does not have a enum keyword like java so enumerations are not quite as smooth. Depending on your requirements there are two ways to make enumerations.
In the following examples notice the use of the sealed keyword when defining the abstract class MyEnum. Sealed specifies that the heirarchy is sealed. Only classes defined in the same file can extend the class.
Also notice in the case object example that the enumeration values are "case object" not "case class". The two are similar except that there is only one instance of a case object. However all the same boiler plate code is generated and you can still match in the same way.
- Create an object that extends the Enumeration class
- Create a case-object hierarchy.
- If you only need discrete and related values without custom behaviour create and object that extends Enumeration
- If each value has custom information associated with it use case-objects
In the following examples notice the use of the sealed keyword when defining the abstract class MyEnum. Sealed specifies that the heirarchy is sealed. Only classes defined in the same file can extend the class.
Also notice in the case object example that the enumeration values are "case object" not "case class". The two are similar except that there is only one instance of a case object. However all the same boiler plate code is generated and you can still match in the same way.
- // Note: MyEnum is an object NOT a class
- scala> object MyEnum extends Enumeration("one", "two", "three") {
- | type MyEnumType = Value
- | val One, Two, Three = Value
- | }
- defined module MyEnum
- scala> MyEnum.One
- res1: MyEnum.Value = one
- scala> def printEnum( value:MyEnum.MyEnumType ) = println( value.toString )
- printEnum: (MyEnum.MyEnumType)Unit
- scala> printEnum(MyEnum.Two)
- two
- // If you don't want to prefix enums with MyEnum. Then you
- // can import the values. This is similar to static imports in java
- scala> import MyEnum._
- import MyEnum._
- scala> def printEnum( value:MyEnumType ) = println( value.toString )
- printEnum: (MyEnum.MyEnumType)Unit
- scala> printEnum(Three)
- three
- // Similar but with cases objects
- // Notice MyEnum is 'sealed' and the parameters have the 'val' keyword so they are public
- scala> abstract sealed class MyEnum(val name:String, val someNum:Int)
- defined class MyEnum
- scala> case object One extends MyEnum("one", 1)
- defined module One
- scala> case object Two extends MyEnum("two", 2)
- defined module Two
- scala> case object Three extends MyEnum("three", 3)
- defined module Three
- scala> def printEnum(value:MyEnum) = println(value.name, value.someNum)
- printEnum: (MyEnum)Unit
- scala> printEnum(One)
- (one,1)
Labels:
beginner,
case-object,
enum,
Enumeration,
object,
Scala
Thursday, August 20, 2009
Object
Scala does not have static methods in the same way that Java does. As a replacement Scala has "objects" and object is a singleton object whose methods can be called in the same manner one would call a static method in Java. The big differentiator is that objects are complete objects and can extent abstract classes and traits.
Objects are sometime referred to as modules as well. See next section for more on modules. In addition there is a special situation where a class has what is called a companion object. That is a topic for another day. Finally you can have case objects, also a topic for another day. case objects will be address as part of the Enumeration topic.
Objects are also a good way to modularize projects. You can define classes and other objects within an object
Objects are sometime referred to as modules as well. See next section for more on modules. In addition there is a special situation where a class has what is called a companion object. That is a topic for another day. Finally you can have case objects, also a topic for another day. case objects will be address as part of the Enumeration topic.
- scala> abstract class SuperClass {
- | def method = "hi"
- | val value = 10
- | }
- defined class SuperClass
- scala> object MyObject extends SuperClass {
- | override def method = "Hello"
- | def anotherMethod = "other"
- | }
- defined module MyObject
- scala> MyObject.method
- res0: java.lang.String = Hello
- scala> MyObject.value
- res1: Int = 10
- scala> MyObject.anotherMethod
- res2: java.lang.String = other
Objects are also a good way to modularize projects. You can define classes and other objects within an object
- scala> object Outer {
- | case class Data(name:String)
- |
- | def print(data:Data) = Console.println(data.name)
- |
- | object Factory {
- | def defaultData = Data("defaultName")
- | }
- | }
- defined module Outer
- scala> val data = Outer.Factory.defaultData
- data: Outer.Data = Data(defaultName)
- scala> Outer.print(data)
- defaultName
Labels:
beginner,
case-object,
object,
Scala
Subscribe to:
Comments (Atom)