diff options
-rw-r--r-- | src/library/scala/Option.scala | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala index 263c9dec37..e40fc22829 100644 --- a/src/library/scala/Option.scala +++ b/src/library/scala/Option.scala @@ -11,13 +11,33 @@ package scala - import Predef._ +import annotation.experimental -object Option { +object Option +{ /** An implicit conversion that converts an option to an iterable value */ implicit def option2Iterable[A](xo: Option[A]): Iterable[A] = xo.toList + + /** An Option factory which creates Some(value) if the argument is not null, + * and None if it is null. + * + * @param x the value + * @return Some(value) if value != null, None if value == null + */ + @experimental + def apply[A](x: A): Option[A] = if (x == null) None else Some(x) + + class NullableOption[A >: Null <: AnyRef](x: Option[A]) { + /** The option's value if it is nonempty, or <code>null</code> if it is empty. + * The use of null of course is discouraged, but code written to use Options + * often must interface with code which expects and returns nulls. + */ + @experimental + def orNull: A = if (x.isEmpty) null else x.get + } + implicit def option2NullableOption[A >: Null <: AnyRef](xo: Option[A]): NullableOption[A] = new NullableOption(xo) } /** This class represents optional values. Instances of <code>Option</code> @@ -92,6 +112,15 @@ sealed abstract class Option[+A] extends Product { if (!isEmpty) f(this.get) } + /** If the given partial function <code>pf</code> is defined for the + * option's value, apply it to the value. Otherwise, None. + * + * @param pf the partial function. + */ + @experimental + def filterMap[B](pf: PartialFunction[Any, B]): Option[B] = + if (!isEmpty && pf.isDefinedAt(this.get)) Some(pf(this.get)) else None + /** If the option is nonempty return it, * otherwise return the result of evaluating an alternative expression. * @param alternative the alternative expression. |