scala.Option

class Option

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
- Matthias Zenger
- Martin Odersky
version
- 1.1, 16/01/2007


Companion: Option

Source: Option.scala(31)

Direct Known Subclasses

None, Some,

 Constructors

def this

 Fields

elements def elements
An singleton iterator returning the option's value if it is nonempty or the empty iterator if the option is empty
Iterator[A]
get abstract def get
get the value of this option [details]
@requires that the option is nonEmpty.
throws
Predef.NoSuchElementException - if the option is empty.
A
isDefined def isDefined
True if the option is a Some( [details]
..) false otherwise.
Boolean
isEmpty abstract def isEmpty
True if the option is the None value, false otherwise
Boolean
productArity abstract def productArity
return k for a product A(x_1, [details]
..,x_k)
Int Product
productPrefix def productPrefix
By default the empty string [details]
Implementations may override this method in order to prepend a string prefix to the result of the toString methods.
String Product
toList def toList
A singleton list containing the option's value if it is nonempty or the empty list if the option is empty
List[A]

 Methods

!= final def !=(arg0 : Any)
o != arg0 is the same as !(o == (arg0)) [details]

param
arg0 - the object to compare against this object for dis-equality.
return
- false if the receiver object is equivalent to the argument; true otherwise.

Boolean Any
!= final def !=(arg0 : Object) Boolean AnyRef
== final def ==(arg0 : Object)
o == arg0 is the same as if (o eq null) arg0 eq null else o [details]
equals(arg0).

param
arg0 - the object to compare against this object for equality.
return
- true if the receiver object is equivalent to the argument; false otherwise.

Boolean AnyRef
== final def ==(arg0 : Any)
o == arg0 is the same as o [details]
equals(arg0).

param
arg0 - the object to compare against this object for equality.
return
- true if the receiver object is equivalent to the argument; false otherwise.

Boolean Any
asInstanceOf final def asInstanceOf[T0]
This method is used to cast the receiver object to be of type T0 [details]

Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression 1.asInstanceOf[String] will throw a ClassCastException at runtime, while the expression List(1).asInstanceOf[List[String]] will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested typed.

return
- the receiver object.

throws
ClassCastException - if the receiver object is not an instance of erasure of type T0.
T0 Any
clone protected def clone
This method creates and returns a copy of the receiver object [details]

The default implementation of the clone method is platform dependent.

return
- a copy of the receiver object.

Object AnyRef
eq final def eq(arg0 : Object)
This method is used to test whether the argument (arg0) is a reference to the receiver object (this) [details]

The eq method implements an equivalence relation on non-null instances of AnyRef:

  • It is reflexive: for any non-null instance x of type AnyRef, x.eq(x) returns true.
  • It is symmetric: for any non-null instances x and y of type AnyRef, x.eq(y) returns true if and only if y.eq(x) returns true.
  • It is transitive: for any non-null instances x, y, and z of type AnyRef if x.eq(y) returns true and y.eq(z) returns true, then x.eq(z) returns true.
Additionally, the eq method has three other properties.
  • It is consistent: for any non-null instances x and y of type AnyRef, multiple invocations of x.eq(y) consistently returns true or consistently returns false.
  • For any non-null instance x of type AnyRef, x.eq(null) and null.eq(x) returns false.
  • null.eq(null) returns true.

When overriding the equals or hashCode methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2), they should be equal to each other (o1 == o2) and they should hash to the same value (o1.hashCode == o2.hashCode).

param
arg0 - the object to compare against this object for reference equality.
return
- true if the argument is a reference to the receiver object; false otherwise.

Boolean AnyRef
equals def equals(arg0 : Any)
This method is used to compare the receiver object (this) with the argument object (arg0) for equivalence [details]

The default implementations of this method is an equivalence relation:

  • It is reflexive: for any instance x of type Any, x.equals(x) should return true.
  • It is symmetric: for any instances x and y of type Any, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any instances x, y, and z of type AnyRef if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

If you override this method, you should verify that your implementation remains an equivalence relation. Additionally, when overriding this method it is often necessary to override hashCode to ensure that objects that are "equal" (o1.equals(o2) returns true) hash to the same Int (o1.hashCode.equals(o2.hashCode)).

param
arg0 - the object to compare against this object for equality.
return
- true if the receiver object is equivalent to the argument; false otherwise.

Boolean AnyRef
filter def filter(p : (A) => Boolean)
If the option is nonempty and the given predicate p yields false on its value, return None [details]
Otherwise return the option value itself.
param
p - the predicate used for testing.
Option[A]
finalize protected def finalize
This method is called by the garbage collector on the receiver object when garbage collection determines that there are no more references to the object [details]

The details of when and if the finalize method are invoked, as well as the interaction between finalize and non-local returns and exceptions, are all platform dependent.

Unit AnyRef
flatMap def flatMap[B](f : (A) => Option[B])
If the option is nonempty, return a function applied to its value [details]
Otherwise return None.
param
f - the function to apply
Option[B]
foreach def foreach(f : (A) => Unit)
Apply the given procedure f to the option's value, if it is nonempty [details]
Do nothing if it is empty.
param
f - the procedure to apply.
Unit
get def get[B >: A](default : B)
[details]
deprecated
- ; use getOrElse instead
B
getClass final def getClass
Returns a representation that corresponds to the dynamic class of the receiver object [details]

The nature of the representation is platform dependent.

return
- a representation that corresponds to the dynamic class of the receiver object.

Class[Any] AnyRef
getOrElse def getOrElse[B >: A](default : => B)
If the option is nonempty return its value, otherwise return the result of evaluating a default expression [details]
param
default - the default expression.
B
hashCode def hashCode
Returns a hash code value for the object [details]

The default hashing algorithm is platform dependent. Note that it is allowed for two objects to have identical hash codes (o1.hashCode.equals(o2.hashCode)) yet not be equal (o1.equals(o2) returns false). A degenerate implementation could always return 0. However, it is required that if two objects are equal (o1.equals(o2) returns true) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)). Therefore, when overriding this method, be sure to verify that the behavior is consistent with the equals method.

return
- the hash code value for the object.

Int AnyRef
isInstanceOf final def isInstanceOf[T0]
This method is used to test whether the dynamic type of the receiver object is T0 [details]

Note that the test result of the test is modulo Scala's erasure semantics. Therefore the expression 1.isInstanceOf[String] will return false, while the expression List(1).isInstanceOf[List[String]] will return true. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested typed.

return
- true if the receiver object is an instance of erasure of type T0; false otherwise.
Boolean Any
map def map[B](f : (A) => B)
If the option is nonempty, return a function applied to its value, wrapped in a Some i [details]
e. Some(f(this.get)). Otherwise return None.
param
f - the function to apply
Option[B]
ne final def ne(arg0 : Object)
o [details]
ne(arg0) is the same as !(o.eq(arg0)).

param
arg0 - the object to compare against this object for reference dis-equality.
return
- false if the argument is not a reference to the receiver object; true otherwise.

Boolean AnyRef
notify final def notify
Wakes up a single thread that is waiting on the receiver object's monitor
Unit AnyRef
notifyAll final def notifyAll
Wakes up all threads that are waiting on the receiver object's monitor
Unit AnyRef
orElse def orElse[B >: A](alternative : => Option[B])
If the option is nonempty return it, otherwise return the result of evaluating an alternative expression [details]
param
alternative - the alternative expression.
Option[B]
productElement abstract def productElement(n : Int)
for a product A(x_1, [details]
..,x_k), returns x_(n+1) for 0 <= n < k
param
n - the index of the element to return
return
- The element n elements after the first element
Any Product
synchronized final def synchronized[T0](arg0 : T0) T0 AnyRef
toLeft def toLeft[X](right : => 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
Either[A, X] with Product
toRight def toRight[X](left : => X)
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
Either[X, A] with Product
toString def toString
Returns a string representation of the object [details]

The default representation is platform dependent.

return
- a string representation of the object.

String AnyRef
wait final def wait Unit AnyRef
wait final def wait(arg0 : Long) Unit AnyRef
wait final def wait(arg0 : Long, arg1 : Int) Unit AnyRef