scala

class Option

[source: scala/Option.scala]

sealed abstract class Option[+A]
extends Product
This class represents optional values. Instances of Option are either instances of case class Some or it is case object None.
Author
Martin Odersky
Matthias Zenger
Version
1.1, 16/01/2007
Direct Known Subclasses:
Some, None

Method Summary
def exists (p : (A) => Boolean) : Boolean
If the option is nonempty, p(value), otherwise false.
def filter (p : (A) => Boolean) : Option[A]
If the option is nonempty and the given predicate p yields false on its value, return None. Otherwise return the option value itself.
def flatMap [B](f : (A) => Option[B]) : Option[B]
If the option is nonempty, return a function applied to its value. Otherwise return None.
def foreach [U](f : (A) => U) : Unit
Apply the given procedure f to the option's value, if it is nonempty. Do nothing if it is empty.
abstract def get : A
get the value of this option. @requires that the option is nonEmpty.
def getOrElse [B >: A](default : => B) : B
If the option is nonempty return its value, otherwise return the result of evaluating a default expression.
def isDefined : Boolean
True if the option is a Some(...) false otherwise.
abstract def isEmpty : Boolean
True if the option is the None value, false otherwise.
def iterator : Iterator[A]
An singleton iterator returning the option's value if it is nonempty or the empty iterator if the option is empty.
def map [B](f : (A) => B) : Option[B]
If the option is nonempty, return a function applied to its value, wrapped in a Some i.e. Some(f(this.get)). Otherwise return None.
def orElse [B >: A](alternative : => Option[B]) : Option[B]
If the option is nonempty return it, otherwise return the result of evaluating an alternative expression.
def orNull [A1 >: A](implicit ev : <:<[Null, A1]) : A1
The option's value if it is nonempty, or null 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.
def partialMap [B](pf : PartialFunction[Any, B]) : Option[B]
If the given partial function pf is defined for the option's value, apply it to the value. Otherwise, None.
def toLeft [X](right : => X) : Product with Either[A, X]
An Either that is a Right with the given argument right if this is empty, or a Left if this is nonempty with the option's value.
def toList : List[A]
A singleton list containing the option's value if it is nonempty or the empty list if the option is empty.
def toRight [X](left : => X) : Product with Either[X, A]
An Either that is a Left with the given argument left if this is empty, or a Right if this is nonempty with the option's value.
Methods inherited from Product
productElement (abstract), productArity (abstract), productIterator, productElements, productPrefix
Methods inherited from AnyRef
getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Methods inherited from Equals
canEqual (abstract)
Method Details
abstract def isEmpty : Boolean
True if the option is the None value, false otherwise.

def isDefined : Boolean
True if the option is a Some(...) false otherwise.

abstract def get : A
get the value of this option. @requires that the option is nonEmpty.
Throws
Predef.NoSuchElementException - if the option is empty.

def getOrElse[B >: A](default : => B) : B
If the option is nonempty return its value, otherwise return the result of evaluating a default expression.
Parameters
default - the default expression.

def orNull[A1 >: A](implicit ev : <:<[Null, A1]) : A1
The option's value if it is nonempty, or null 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.

def map[B](f : (A) => B) : Option[B]
If the option is nonempty, return a function applied to its value, wrapped in a Some i.e. Some(f(this.get)). Otherwise return None.
Parameters
f - the function to apply

def flatMap[B](f : (A) => Option[B]) : Option[B]
If the option is nonempty, return a function applied to its value. Otherwise return None.
Parameters
f - the function to apply

def filter(p : (A) => Boolean) : Option[A]
If the option is nonempty and the given predicate p yields false on its value, return None. Otherwise return the option value itself.
Parameters
p - the predicate used for testing.

def exists(p : (A) => Boolean) : Boolean
If the option is nonempty, p(value), otherwise false.
Parameters
p - the predicate to test

def foreach[U](f : (A) => U) : Unit
Apply the given procedure f to the option's value, if it is nonempty. Do nothing if it is empty.
Parameters
f - the procedure to apply.

def partialMap[B](pf : PartialFunction[Any, B]) : Option[B]
If the given partial function pf is defined for the option's value, apply it to the value. Otherwise, None.
Parameters
pf - the partial function.

def orElse[B >: A](alternative : => Option[B]) : Option[B]
If the option is nonempty return it, otherwise return the result of evaluating an alternative expression.
Parameters
alternative - the alternative expression.

def iterator : Iterator[A]
An singleton iterator returning the option's value if it is nonempty or the empty iterator if the option is empty.

def toList : List[A]
A singleton list containing the option's value if it is nonempty or the empty list if the option is empty.

def toRight[X](left : => X) : Product with Either[X, A]
An Either that is a Left with the given argument left if this is empty, or a Right if this is nonempty with the option's value.

def toLeft[X](right : => X) : Product with Either[A, X]
An Either that is a Right with the given argument right if this is empty, or a Left if this is nonempty with the option's value.