tags:

views:

136

answers:

1

Something like

def option[T](v: T): Option[T] = if (v == null) None else Some(v)

I'm perfectly happy defining this utility method myself, but just wondered if it already exists somewhere.

+10  A: 
scala> Option(null)
res0: Option[Null] = None

scala> Option(1)
res1: Option[Int] = Some(1)
Alexey
Not that `Option#apply` is new in Scala 2.8. Also new is the inverse operation, `Option#orNull`.
retronym
bummer, I'm using 2.7.7
Aaron Novstrup
I would also absolutely love this in 2.7.7. You can roll your own quite easily though.
Justin W