Possible Duplicate:
Why Option[T] ?
I am a newbie on Scala and I can't really feel that sensation of that difference between null on java and Option
on Scala .
I know that none is an object and if I write smth like that on Scala , it will go safe :
val map = Map("koko" -> "is a cat")
val string:Option[String] =map.get("other")
println(string.map(a=>println(a.toString)) )
I well get None
as a result instead of throwing an exception .
It's interesting .
However if I need the returning value not to be wrapped by Some
. I will use .get to return the value .
In our example , it will throw an exceoption:
map.get("other").get.map(a=>println(a.toString))
I know I can handle this issue by using "match" . I am taking here that I need to learn what's that booming on option on Scala vs null on java !
What I still couldn't get is that how could I use the advantages of Option but in case there are values existed into my variable , return the value not Some(value)
def testOption(): String = {
val map = Map("koko" -> "is a cat")
val string: Option[String] = map.get("koko")
string
}
I mean is there any way to make this code works instead of updating the return value to be Option[String]
!
Imagine that I have to return a String in order to be set into my bean variable that is of type String not Option[String]
How could I make that works without any kind of match
!!
I guess if there smth that makes it works , that will make me understand well the power of Option
.