Home > Uncategorized > Scala [Some Option None]

Scala [Some Option None]

I personally think to work with LiftWeb framework basic Scala  knowledge is very important. Till today I really struggle to understand some pieces of code in LiftWeb.

For a change, I would like to write blog about few important classes of Scala and LiftWeb while I learn about them.

Classes which I would like to cover them are Option, Some, None, Full and Box. Last two classes are available in LiftWebCommon.

scala> val i = Some(5)
i: Some[Int] = Some(5)

Observation is Some container, it can hold any other type.

scala> None
res34: None.type = None

None is Singleton object.

scala> Option
res36: Option.type = scala.Option$@f3b83

Option is again Singleton object

scala> var t = Option
t: object Option = scala.Option$@f3b83

scala> t.getClass().getMethods().map(println(_))
public int scala.Option$.$tag() throws java.rmi.RemoteException
public scala.Iterable scala.Option$.option2Iterable(scala.Option)
public final native void java.lang.Object.notifyAll()
res38: Array[Unit] = Array((), (), (), (), (), (), (), (), (), (), ())

Here are my observation, Option is a container, which holds some other value. None is special type of Option, which is Singleton, which represents Container which has no value, kind of null in Java.

Some is again special type of Option, which holds value. Option also is a singleton object, which has a utility method called option2Iterable.

They are heavily used when pattern matching is applied. It makes code more elegant. In simple, wherever y0u use null check in Java, you have to consider using above type of classes by encapsulating actual result using Some or None, and comparing with Some(x) or None.

Let us consider the following method, whenever we invoke rollDice method, either it may return Integer value or unexpected value called None. Wherever we need to check if it could be zero, we can just compare it with None.

scala> def rollDice:Option[Int]  = {
|   Some(Math.random*6 toInt) match {
| case Some(0)=>return None
| case Some(x)=> return Some(x)
|   }
| }
rollDice: Option[Int]

scala> rollDice
res13: Option[Int] = Some(3)

scala> rollDice
res17: Option[Int] = None

Let us write some piece of code which invokes the rollDice method.

rollDice match {
case None        => println(“Failed Roll”);
case Some(x)     => println(“I have got the valid value :”  + x);

If you observe the other method, which is not comparing either zero or null, rather it compares with None.

Some Scala utility methods which I am using for learning

scala> def printMethods(x:String):Unit = {
|                   Class.forName(x).getMethods().map(println(_))
| }
printMethods: (String)Unit

The above is used to find few methods available in classes, I may not use them in this blog.

Let us continue remaining in next blog…

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: