Option[Something] type is really useful in Scala language while it can help you to avoid some NullPointerExceptions errors and probably necessary checks. “Option” is one of the simpliest and basic types in Scala, so if you want to learn Scala - you should know how it works. A very simple explanation is given here. And apparently you can google for more resources if you want.

So, I just want you to show a small basisc snippet that can be efficiently used in your Play2 view templates (Play! is a powerfull scala framework) :

@(maybeUser: Option[User])
stuff that everyone sees
@maybeUser.map { user =>
   stuff that should be shown only to logged in users
}

Map? What? Yes! It is a common pattern to extract values from Option[Class] container. So the advice here - to extract values exactly in place or right before it is needed.

While I am just a newbie in Scala and Play! framework my source code is full of the following constructs :

val str:String = something.map { val => val.toString() } getOrElse "";
...
other code

Also, depending on next lines of code you should write something like this

something.map { val =>
    ...a portion of code
} getOrElse NotFound

It is very useful and good approach as if someting not equals Some(value), but equals nothing, i.e. None. Probably there are other nice patterns to use in Scala, but I’ll show them to you some time later!