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.
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.
the value being probed for possible equality
true if this instance can possibly equal that
, otherwise false
Returns the option's value.
Returns the option's value.
java.util.NoSuchElementException
if the option is empty.
The option must be nonEmpty.
Returns true if the option is None
, false otherwise.
The size of this product.
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
.
the index of the element to return
the element n
elements after the first element
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.
the partial function.
the result of applying pf
to this scala.Option's
value (if possible), or None
.
// 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}
Tests whether the option contains a given value as an element.
Tests whether the option contains a given value as an element.
the element to test.
true
if the option has an element that is equal (as
determined by ==
) to elem
, false
otherwise.
// 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"
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.
the predicate to test
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
.
the predicate used for testing.
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
.
the predicate used for testing.
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
).
the function to apply
foreach
map
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
.
the expression to evaluate if empty.
the function to apply if nonempty.
This is equivalent to scala.Option map f getOrElse ifEmpty
.
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.
the predicate to test
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.
the procedure to apply.
flatMap
map
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
.
the default expression.
Returns true if the option is an instance of scala.Some, false otherwise.
Returns a singleton iterator returning the scala.Option's value if it is nonempty, or an empty iterator if the option is empty.
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
.
the function to apply
This is similar to flatMap
except here,
f
does not need to wrap its result in an scala.Option.
foreach
flatMap
Returns false if the option is None
, true otherwise.
Returns false if the option is None
, true otherwise.
Implemented here to avoid the implicit conversion to Iterable.
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
.
the alternative expression.
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.
val initialText: Option[String] = getInitialText val textField = new JComponent(initialText.orNull,20)
An iterator over all the elements of this product.
An iterator over all the elements of this product.
in the default implementation, an Iterator[Any]
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.
in the default implementation, the empty string
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.
the expression to evaluate and return if this is empty
toRight
Returns a singleton list containing the scala.Option's value if it is nonempty, or the empty list if the scala.Option is empty.
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.
the expression to evaluate and return if this is empty
toLeft
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.
Represents optional values. Instances of
Option
are either an instance of scala.Some or the objectNone
.The most idiomatic way to use an scala.Option instance is to treat it as a collection or monad and use
map
,flatMap
,filter
, orforeach
:Note that this is equivalent to
Because of how for comprehension works, if
None
is returned fromrequest.getParameter
, the entire expression results inNone
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:
1.1, 16/01/2007
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.