In this blogpost by James Iry, he writes:
Plus, Scala has an "option" method that promotes a value to either Some(value) or None depending on whether it's null or not ...
I can't seem to find this option
method anywhere in the scaladoc.
Iulian Dragos's gdata client project contains a method that is probably what James was referring to.
def option[A <: AnyRef](a: A): Option[A] =
if (a eq null) None else Some(a)
Please point out where can I find this method in the scaladoc.
P.S. I have a method that looks like this:
def permutations(s: String): List[String] = ...
I'm in 2 minds as to whether I should change it to:
def permutations(s: Option[String]): List[String] = ...
since the client can invoke it with null
. Currently in the first instance, I expect a String parameter & I box it manually using the option
method mentioned previously.
You can simply use the apply factory method on its companion object:
... or the shorter version: