Option

scalus.prelude.Option
See theOption companion object
enum Option[+A]

Alternative to scala.Option in onchain code.

Options Eq and Ord are defined in terms of the contained A elements.

Type parameters

A

the type of the element contained in the Option

Attributes

Companion
object
Graph
Supertypes
trait Enum
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
Known subtypes
class Some[A]

Members list

Type members

Enum entries

case None extends Option[Nothing]
final case class Some[+A](value: A) extends Option[A]

Value members

Concrete methods

def asScala: Option[A]
Extension method from Option

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
Extension method from Option

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
Extension method from Option

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]
Extension method from Option

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]
Extension method from Option

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]
Extension method from Option

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]
Extension method from Option

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 flatten: Option[A]
Extension method from Option

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
def forall(p: A => Boolean): Boolean
Extension method from Option

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
Extension method from Option

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
Extension method from Option

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
Extension method from Option

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
Extension method from Option

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
Extension method from Option

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]
Extension method from Option

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
Extension method from Option

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]
Extension method from Option

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
Extension method from Option

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