Class/Object

scala

Option

Related Docs: object Option | package scala

Permalink

sealed abstract class Option[+A] extends Product with Serializable

Represents optional values. Instances of Option are either an instance of scala.Some or the object None.

The most idiomatic way to use an scala.Option instance is to treat it as a collection or monad and use map,flatMap, filter, or foreach:

val name: Option[String] = request getParameter "name"
val upper = name map { _.trim } filter { _.length != 0 } map { _.toUpperCase }
println(upper getOrElse "")

Note that this is equivalent to

val upper = for {
  name <- request getParameter "name"
  trimmed <- Some(name.trim)
  upper <- Some(trimmed.toUpperCase) if trimmed.length != 0
} yield upper
println(upper getOrElse "")

Because of how for comprehension works, if None is returned from request.getParameter, the entire expression results in None

This allows for sophisticated chaining of scala.Option values without having to check for the existence of a value.

A less-idiomatic way to use scala.Option values is via pattern matching:

val nameMaybe = request getParameter "name"
nameMaybe match {
  case Some(name) =>
    println(name.trim.toUppercase)
  case None =>
    println("No name value")
}
Self Type
Option[A]
Annotations
@SerialVersionUID()
Version

1.1, 16/01/2007

Note

Many of the methods in here are duplicative with those in the Traversable hierarchy, but they are duplicated for a reason: the implicit conversion tends to leave one with an Iterable in situations where one could have retained an Option.

Linear Supertypes
Serializable, java.io.Serializable, Product, Equals, AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Option
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. AnyRef
  7. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. class WithFilter extends AnyRef

    Permalink

    We need a whole WithFilter class to honor the "doesn't create a new collection" contract even though it seems unlikely to matter much in a collection with max size 1.

Abstract Value Members

  1. abstract def canEqual(that: Any): Boolean

    Permalink

    A method that should be called from every well-designed equals method that is open to be overridden in a subclass.

    A method that should be called from every well-designed equals method that is open to be overridden in a subclass. See Programming in Scala, Chapter 28 for discussion and design.

    that

    the value being probed for possible equality

    returns

    true if this instance can possibly equal that, otherwise false

    Definition Classes
    Equals
  2. abstract def get: A

    Permalink

    Returns the option's value.

    Returns the option's value.

    Exceptions thrown

    java.util.NoSuchElementException if the option is empty.

    Note

    The option must be nonEmpty.

  3. abstract def isEmpty: Boolean

    Permalink

    Returns true if the option is None, false otherwise.

  4. abstract def productArity: Int

    Permalink

    The size of this product.

    The size of this product.

    returns

    for a product A(x1, ..., xk), returns k

    Definition Classes
    Product
  5. abstract def productElement(n: Int): Any

    Permalink

    The nth element of this product, 0-based.

    The nth element of this product, 0-based. In other words, for a product A(x1, ..., xk), returns x(n+1) where 0 < n < k.

    n

    the index of the element to return

    returns

    the element n elements after the first element

    Definition Classes
    Product
    Exceptions thrown

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def collect[B](pf: PartialFunction[A, B]): Option[B]

    Permalink

    Returns a scala.Some containing the result of applying pf to this scala.Option's contained value, if this option is nonempty and pf is defined for that value.

    Returns a scala.Some containing the result of applying pf to this scala.Option's contained value, if this option is nonempty and pf is defined for that value. Returns None otherwise.

    pf

    the partial function.

    returns

    the result of applying pf to this scala.Option's value (if possible), or None.

    Annotations
    @inline()
    Example:
    1. // Returns Some(HTTP) because the partial function covers the case.
      Some("http") collect {case "http" => "HTTP"}
      // Returns None because the partial function doesn't cover the case.
      Some("ftp") collect {case "http" => "HTTP"}
      // Returns None because the option is empty. There is no value to pass to the partial function.
      None collect {case value => value}
  7. final def contains[A1 >: A](elem: A1): Boolean

    Permalink

    Tests whether the option contains a given value as an element.

    Tests whether the option contains a given value as an element.

    elem

    the element to test.

    returns

    true if the option has an element that is equal (as determined by ==) to elem, false otherwise.

    Example:
    1. // Returns true because Some instance contains string "something" which equals "something".
      Some("something") contains "something"
      // Returns false because "something" != "anything".
      Some("something") contains "anything"
      // Returns false when method called on None.
      None contains "anything"
  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. final def exists(p: (A) ⇒ Boolean): Boolean

    Permalink

    Returns true if this option is nonempty and the predicate p returns true when applied to this scala.Option's value.

    Returns true if this option is nonempty and the predicate p returns true when applied to this scala.Option's value. Otherwise, returns false.

    p

    the predicate to test

    Annotations
    @inline()
  11. final def filter(p: (A) ⇒ Boolean): Option[A]

    Permalink

    Returns this scala.Option if it is nonempty and applying the predicate p to this scala.Option's value returns true.

    Returns this scala.Option if it is nonempty and applying the predicate p to this scala.Option's value returns true. Otherwise, return None.

    p

    the predicate used for testing.

    Annotations
    @inline()
  12. final def filterNot(p: (A) ⇒ Boolean): Option[A]

    Permalink

    Returns this scala.Option if it is nonempty and applying the predicate p to this scala.Option's value returns false.

    Returns this scala.Option if it is nonempty and applying the predicate p to this scala.Option's value returns false. Otherwise, return None.

    p

    the predicate used for testing.

    Annotations
    @inline()
  13. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  14. final def flatMap[B](f: (A) ⇒ Option[B]): Option[B]

    Permalink

    Returns the result of applying f to this scala.Option's value if this scala.Option is nonempty.

    Returns the result of applying f to this scala.Option's value if this scala.Option is nonempty. Returns None if this scala.Option is empty. Slightly different from map in that f is expected to return an scala.Option (which could be None).

    f

    the function to apply

    Annotations
    @inline()
    See also

    foreach

    map

  15. def flatten[B](implicit ev: <:<[A, Option[B]]): Option[B]

    Permalink
  16. final def fold[B](ifEmpty: ⇒ B)(f: (A) ⇒ B): B

    Permalink

    Returns the result of applying f to this scala.Option's value if the scala.Option is nonempty.

    Returns the result of applying f to this scala.Option's value if the scala.Option is nonempty. Otherwise, evaluates expression ifEmpty.

    ifEmpty

    the expression to evaluate if empty.

    f

    the function to apply if nonempty.

    Annotations
    @inline()
    Note

    This is equivalent to scala.Option map f getOrElse ifEmpty.

  17. final def forall(p: (A) ⇒ Boolean): Boolean

    Permalink

    Returns true if this option is empty or the predicate p returns true when applied to this scala.Option's value.

    Returns true if this option is empty or the predicate p returns true when applied to this scala.Option's value.

    p

    the predicate to test

    Annotations
    @inline()
  18. final def foreach[U](f: (A) ⇒ U): Unit

    Permalink

    Apply the given procedure f to the option's value, if it is nonempty.

    Apply the given procedure f to the option's value, if it is nonempty. Otherwise, do nothing.

    f

    the procedure to apply.

    Annotations
    @inline()
    See also

    flatMap

    map

  19. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  20. final def getOrElse[B >: A](default: ⇒ B): B

    Permalink

    Returns the option's value if the option is nonempty, otherwise return the result of evaluating default.

    Returns the option's value if the option is nonempty, otherwise return the result of evaluating default.

    default

    the default expression.

    Annotations
    @inline()
  21. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  22. def isDefined: Boolean

    Permalink

    Returns true if the option is an instance of scala.Some, false otherwise.

  23. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  24. def iterator: Iterator[A]

    Permalink

    Returns a singleton iterator returning the scala.Option's value if it is nonempty, or an empty iterator if the option is empty.

  25. final def map[B](f: (A) ⇒ B): Option[B]

    Permalink

    Returns a scala.Some containing the result of applying f to this scala.Option's value if this scala.Option is nonempty.

    Returns a scala.Some containing the result of applying f to this scala.Option's value if this scala.Option is nonempty. Otherwise return None.

    f

    the function to apply

    Annotations
    @inline()
    Note

    This is similar to flatMap except here, f does not need to wrap its result in an scala.Option.

    See also

    foreach

    flatMap

  26. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  27. final def nonEmpty: Boolean

    Permalink

    Returns false if the option is None, true otherwise.

    Returns false if the option is None, true otherwise.

    Note

    Implemented here to avoid the implicit conversion to Iterable.

  28. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  29. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  30. final def orElse[B >: A](alternative: ⇒ Option[B]): Option[B]

    Permalink

    Returns this scala.Option if it is nonempty, otherwise return the result of evaluating alternative.

    Returns this scala.Option if it is nonempty, otherwise return the result of evaluating alternative.

    alternative

    the alternative expression.

    Annotations
    @inline()
  31. final def orNull[A1 >: A](implicit ev: <:<[Null, A1]): A1

    Permalink

    Returns the option's value if it is nonempty, or null if it is empty.

    Returns the option's value if it is nonempty, or null if it is empty. Although the use of null is discouraged, code written to use scala.Option must often interface with code that expects and returns nulls.

    Annotations
    @inline()
    Example:
    1. val initialText: Option[String] = getInitialText
      val textField = new JComponent(initialText.orNull,20)
  32. def productIterator: Iterator[Any]

    Permalink

    An iterator over all the elements of this product.

    An iterator over all the elements of this product.

    returns

    in the default implementation, an Iterator[Any]

    Definition Classes
    Product
  33. def productPrefix: String

    Permalink

    A string used in the toString methods of derived classes.

    A string used in the toString methods of derived classes. Implementations may override this method to prepend a string prefix to the result of toString methods.

    returns

    in the default implementation, the empty string

    Definition Classes
    Product
  34. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  35. final def toLeft[X](right: ⇒ X): Product with Serializable with util.Either[A, X]

    Permalink

    Returns a scala.util.Right containing the given argument right if this is empty, or a scala.util.Left containing this scala.Option's value if this scala.Option is nonempty.

    Returns a scala.util.Right containing the given argument right if this is empty, or a scala.util.Left containing this scala.Option's value if this scala.Option is nonempty.

    right

    the expression to evaluate and return if this is empty

    Annotations
    @inline()
    See also

    toRight

  36. def toList: List[A]

    Permalink

    Returns a singleton list containing the scala.Option's value if it is nonempty, or the empty list if the scala.Option is empty.

  37. final def toRight[X](left: ⇒ X): Product with Serializable with util.Either[X, A]

    Permalink

    Returns a scala.util.Left containing the given argument left if this scala.Option is empty, or a scala.util.Right containing this scala.Option's value if this is nonempty.

    Returns a scala.util.Left containing the given argument left if this scala.Option is empty, or a scala.util.Right containing this scala.Option's value if this is nonempty.

    left

    the expression to evaluate and return if this is empty

    Annotations
    @inline()
    See also

    toLeft

  38. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  39. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  40. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  41. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  42. final def withFilter(p: (A) ⇒ Boolean): WithFilter

    Permalink

    Necessary to keep scala.Option from being implicitly converted to scala.collection.Iterable in for comprehensions.

    Necessary to keep scala.Option from being implicitly converted to scala.collection.Iterable in for comprehensions.

    Annotations
    @inline()

Inherited from Serializable

Inherited from java.io.Serializable

Inherited from Product

Inherited from Equals

Inherited from AnyRef

Inherited from Any

Ungrouped