Packages

  • package root

    This is the documentation for the Scala standard library.

    This is the documentation for the Scala standard library.

    Package structure

    The scala package contains core types like Int, Float, Array or Option which are accessible in all Scala compilation units without explicit qualification or imports.

    Notable packages include:

    Other packages exist. See the complete list on the right.

    Additional parts of the standard library are shipped as separate libraries. These include:

    Automatic imports

    Identifiers in the scala package and the scala.Predef object are always in scope by default.

    Some of these identifiers are type aliases provided as shortcuts to commonly used classes. For example, List is an alias for scala.collection.immutable.List.

    Other aliases refer to classes provided by the underlying platform. For example, on the JVM, String is an alias for java.lang.String.

    Definition Classes
    root
  • package scala

    Core Scala types.

    Core Scala types. They are always available without an explicit import.

    Definition Classes
    root
  • sealed abstract class Option[+A] extends IterableOnce[A] with Product with Serializable

    Represents optional values.

    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.

    These are useful methods that exist for both scala.Some and None.

    • isDefined — True if not empty
    • isEmpty — True if empty
    • nonEmpty — True if not empty
    • orElse — Evaluate and return alternate optional value if empty
    • getOrElse — Evaluate and return alternate value if empty
    • get — Return value, throw exception if empty
    • fold — Apply function on optional value, return default if empty
    • map — Apply a function on the optional value
    • flatMap — Same as map but function must return an optional value
    • foreach — Apply a procedure on option value
    • collect — Apply partial pattern match on optional value
    • filter — An optional value satisfies predicate
    • filterNot — An optional value doesn't satisfy predicate
    • exists — Apply predicate on optional value, or false if empty
    • forall — Apply predicate on optional value, or true if empty
    • contains — Checks if value equals optional value, or false if empty
    • zip — Combine two optional values to make a paired optional value
    • unzip — Split an optional pair to two optional values
    • unzip3 — Split an optional triple to three optional values
    • toList — Unary list of optional value, otherwise the empty list

    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")
    }

    Interacting with code that can occasionally return null can be safely wrapped in scala.Option to become None and scala.Some otherwise.

    val abc = new java.util.HashMap[Int, String]
    abc.put(1, "A")
    bMaybe = Option(abc.get(2))
    bMaybe match {
      case Some(b) =>
        println(s"Found $b")
      case None =>
        println("Not found")
    }
    Definition Classes
    scala
    Annotations
    @SerialVersionUID()
    Note

    Many of the methods in here are duplicative with those in the Iterable 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.

  • WithFilter
c

scala.Option

WithFilter

class WithFilter extends AnyRef

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.

Source
Option.scala
Linear Supertypes
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. WithFilter
  2. AnyRef
  3. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new WithFilter(p: (A) => Boolean)

Value Members

  1. def flatMap[B](f: (A) => Option[B]): Option[B]
  2. def foreach[U](f: (A) => U): Unit
  3. def map[B](f: (A) => B): Option[B]
  4. def withFilter(q: (A) => Boolean): WithFilter