Option

scalus.prelude.Option
See theOption companion enum
object Option

Attributes

Companion
enum
Graph
Supertypes
trait Sum
trait Mirror
class Object
trait Matchable
class Any
Self type
Option.type

Members list

Type members

Inherited and Abstract types

type MirroredElemLabels <: Tuple

The names of the product elements

The names of the product elements

Attributes

Inherited from:
Mirror
type MirroredLabel <: String

The name of the type

The name of the type

Attributes

Inherited from:
Mirror

Value members

Concrete methods

inline def apply[A](x: A): Option[A]

Constructs an Option from a value. If the value is null, returns None, otherwise Some(value).

Constructs an Option from a value. If the value is null, returns None, otherwise Some(value).

Attributes

Returns

Some(value) if value != null, None if value == null

Example
 Option.apply(null) == Option.None
 Option.apply("1").get == "1"
inline def empty[A]: Option[A]

Returns an empty Option instance.

Returns an empty Option instance.

Attributes

Returns

an empty Option instance (None)

Example
 Option.empty == Option.apply(null)

Givens

Givens

given emptyOptionEq: () => Option[Nothing]
given optionEq[A](using eq: () => A): () => Option[A]
given optionOrd[A](using ord: Ord[A]): Ord[Option[A]]
given optionToData[A : ToData]: ToData[Option[A]]

Extensions

Extensions

extension [A](self: Option[Option[A]])
def flatten: Option[A]

Returns the inner Option if this Option contains one, otherwise returns None.

Returns the inner Option if this Option contains one, otherwise returns None.

Attributes

Returns

the inner Option if this Option contains a Some, otherwise None

Example
 Option.Some(Option.Some("hello")).flatten == Option.Some("hello")
 Option.Some(Option.None).flatten == Option.None
 Option.None.flatten == Option.None
extension [A](self: Option[A])
def asScala: Option[A]

Converts this Option to a scala.Option.

Converts this Option to a scala.Option.

Attributes

Returns

a scala.Some if this Option contains a value, scala.None otherwise

Example
 Option.Some("hello").asScala == scala.Some("hello")
 Option.None.asScala == scala.None
def contains[B >: A](elem: B)(using eq: () => B): Boolean

Checks if this Option contains the specified element.

Checks if this Option contains the specified element.

Value parameters

elem

the element to check for

eq

the equality comparison to use

Attributes

Returns

true if this Option contains a value equal to elem, false otherwise

Example
 Option.Some("hello").contains("hello") == true
 Option.Some("hello").contains("world") == false
 Option.None.contains("hello") == false
def exists(p: A => Boolean): Boolean

Checks if this Option contains a value that satisfies the given condition.

Checks if this Option contains a value that satisfies the given condition.

Value parameters

p

the condition to test the value against

Attributes

Returns

true if this Option contains a value that satisfies the condition, false otherwise

Example
 Option.Some(BigInt(5)).exists(_ > BigInt(3)) == true
 Option.Some(BigInt(2)).exists(_ > BigInt(3)) == false
 Option.None.exists(_ > BigInt(3)) == false
def filter(predicate: A => Boolean): Option[A]

Returns this Option if its value satisfies the given condition, otherwise returns None.

Returns this Option if its value satisfies the given condition, otherwise returns None.

Value parameters

predicate

the condition to test the value against

Attributes

Returns

this Option if it contains a value that satisfies the predicate, otherwise None

See also
Example
 Option.Some(BigInt(5)).filter(_ > BigInt(3)) == Option.Some(BigInt(5))
 Option.Some(BigInt(2)).filter(_ > BigInt(3)) == Option.None
 Option.None.filter(_ > BigInt(3)) == Option.None
def filterNot(predicate: A => Boolean): Option[A]

Returns this Option if its value does not satisfy the given condition, otherwise returns None.

Returns this Option if its value does not satisfy the given condition, otherwise returns None.

Value parameters

predicate

the condition to test the value against

Attributes

Returns

this Option if it contains a value that does not satisfy the predicate, otherwise None

See also
Example
 Option.Some(BigInt(2)).filterNot(_ > BigInt(3)) == Option.Some(BigInt(2))
 Option.Some(BigInt(5)).filterNot(_ > BigInt(3)) == Option.None
 Option.None.filterNot(_ > 3) == Option.None
inline def find(p: A => Boolean): Option[A]

Returns this Option if its value satisfies the given condition, otherwise returns None.

Returns this Option if its value satisfies the given condition, otherwise returns None.

Value parameters

p

the condition to test the value against

Attributes

Returns

this Option if it contains a value that satisfies the condition, otherwise None

See also
Example
 Option.Some(BigInt(5)).find(_ > BigInt(3)) == Option.Some(BigInt(5))
 Option.Some(BigInt(2)).find(_ > BigInt(3)) == Option.None
 Option.None.find(_ > BigInt(3)) == Option.None
def flatMap[B](mapper: A => Option[B]): Option[B]

Applies a function to the value inside this Option and returns the result directly.

Applies a function to the value inside this Option and returns the result directly.

Value parameters

mapper

the function to apply to the value of this Option, returning an Option

Attributes

Returns

the Option returned by the mapper function if this Option contains a value, otherwise None

See also

map

Example
 Option.Some(BigInt(5)).flatMap(x => if x > BigInt(0) then Option.Some(x * BigInt(2)) else Option.None) == Option.Some(BigInt(10))
 Option.Some(BigInt(-1)).flatMap(x => if x > BigInt(0) then Option.Some(x * BigInt(2)) else Option.None) == Option.None
 Option.None.flatMap(x => Option.Some(x * BigInt(2))) == Option.None
def forall(p: A => Boolean): Boolean

Checks if this Option is empty or its value satisfies the given condition.

Checks if this Option is empty or its value satisfies the given condition.

Value parameters

p

the condition to test the value against

Attributes

Returns

true if this Option is empty or contains a value that satisfies the condition, false otherwise

Example
 Option.Some(BigInt(5)).forall(_ > BigInt(3)) == true
 Option.Some(BigInt(2)).forall(_ > BigInt(3)) == false
 Option.None.forall(_ > BigInt(3)) == true
def get: A

Returns the value if this Option contains one, otherwise throws an exception.

Returns the value if this Option contains one, otherwise throws an exception.

Attributes

Returns

the value contained in this Option

Throws
NoSuchElementException

if this Option is None

See also
Example
 Option.Some("hello").get == "hello"
 try {
   Option.None.get
 } catch {
   case e: NoSuchElementException => e.getMessage == "None.get"
 }
def getOrElse[B >: A](default: B): B

Returns the value if this Option contains one, otherwise returns the default value.

Returns the value if this Option contains one, otherwise returns the default value.

Value parameters

default

the value to return if this Option is None

Attributes

Returns

the value of this Option if it contains one, otherwise the default value

Example
 Option.Some("hello").getOrElse("world") == "hello"
 Option.None.getOrElse("world") == "world"
inline def getOrFail(inline message: String): A

Returns the value if this Option contains one, otherwise throws an exception.

Returns the value if this Option contains one, otherwise throws an exception.

Value parameters

message

message to include in the exception if this Option is None

Attributes

Returns

the value contained in this Option

Throws
NoSuchElementException

if this Option is None

Example
 Option.Some("hello").getOrFail() == "hello"
 try {
   Option.None.getOrFail("custom message")
 } catch {
   case e: NoSuchElementException => e.getMessage == "custom message"
 }
inline def isDefined: Boolean

Checks if this Option contains a value.

Checks if this Option contains a value.

Attributes

Returns

true if this Option contains a value, false if it is None

See also
def isEmpty: Boolean

Checks if this Option is empty.

Checks if this Option is empty.

Attributes

Returns

true if this Option is None, false if it contains a value

Example
 Option.empty.isEmpty == true
 Option.apply("1").isEmpty == false
 Option.apply(null).isEmpty == true
def map[B](mapper: A => B): Option[B]

Applies a function to the value inside this Option.

Applies a function to the value inside this Option.

Value parameters

mapper

the function to apply to the value of this Option

Attributes

Returns

a Some containing the result if this Option contains a value, otherwise None

See also
Example
 Option.Some(BigInt(5)).map(_ * BigInt(2)) == Option.Some(BigInt(10))
 Option.None.map(_ * BigInt(2)) == Option.None
 Option.Some("hello").map(_.toUpperCase) == Option.Some("HELLO")
inline def nonEmpty: Boolean

Checks if this Option contains a value.

Checks if this Option contains a value.

Attributes

Returns

true if this Option contains a value, false if it is None

Example
 Option.empty.nonEmpty == false
 Option.apply("1").nonEmpty == true
 Option.apply(null).nonEmpty == false
def orElse[B >: A](alternative: Option[B]): Option[B]

Returns this Option if it contains a value, otherwise returns the alternative Option.

Returns this Option if it contains a value, otherwise returns the alternative Option.

Value parameters

alternative

the Option to return if this Option is None

Attributes

Returns

this Option if it contains a value, otherwise the alternative Option

Example
 Option.Some("hello").orElse(Option.Some("world")) == Option.Some("hello")
 Option.None.orElse(Option.Some("world")) == Option.Some("world")
 Option.None.orElse(Option.None) == Option.None
infix inline def orFail(inline message: String): Unit

Throws an exception if this Option is None, otherwise does nothing.

Throws an exception if this Option is None, otherwise does nothing.

Value parameters

message

message to include in the exception if this Option is None

Attributes

Throws
NoSuchElementException

if this Option is None

Example
 Option.Some("hello") orFail "Should not throw" == ()
 try {
   Option.None orFail "custom message"
 } catch {
   case e: NoSuchElementException => e.getMessage == "custom message"
 }