Opt

opaque type Opt[+A]

Value Option

Opt is a container, which can hold a single value or be empty

Option can be converted to value with method or, which also takes default value for the case when option is empty

Option is often used as Boolean, where ''Option'' with value is converted to 'true' and empty option is converted to 'false'

val l: Lookup[Int,String] = ???

val o: Opt[String] = l.getOpt(12)

if(o) println("Value found.")
else  println("Value not found.")
Source
__.scala

Def

inline def ~~: Stream[A]

Alias to "stream"

Alias to "stream"

Source
__.scala
inline def collect[B](f: scala.PartialFunction[A, B]): Opt[B]

Value filter and converter

Value filter and converter

Discards value if given partial function is not defined

Otherwise value is converted to the function result

  val o: Opt[String] = "foo"

  o.collect { case v if v.startsWith("a") => "bar" }.tp // Prints: \/

  o.collect { case v if v.startsWith("f") => "bar" }.tp // Prints: Opt(bar)

Note: If Opt is empty, it is returned as is

Note: collect is similar to mapOpt, but is less efficient, because PartialFunction has to be evaluated twice

Source
__.scala
inline def contains(value: A): Boolean

Check contains

Check contains

Returns 'true' if option contains given value

'false' - otherwise

  val o : Opt[String] = "foo"

  o.contains("foo").tp  // Prints: true

  o.contains("bar").tp  // Prints: false
Source
__.scala
inline def default(dv: => A): Opt[A]

Default value

Default value

Does nothing if option already contains a value

For void option, returns a new option with given value

   var o : Opt[String] = "foo"

   o.default("bar").tp // Prints: Opt(foo)

   o=VOID

   o.default("bar").tp // Prints: Opt(bar)
Source
__.scala
inline def drop(f: A => Boolean): Opt[A]

Reversed filter

Reversed filter

Discards value if it satisfies given predicate

 val o : Opt[String] = "foo"

 o.drop(_.length > 2).tp  // Prints: Opt(VOID)

 o.drop(_.length > 3).tp  // Prints: Opt(foo)
Source
__.scala
inline def dropOnly(v: A): Opt[A]

Reversed value filter

Reversed value filter

Discards value if it is equal to given value

 val o : Opt[String] = "foo"

 o.dropOnly("foo").tp  // Prints: Opt(VOID)

 o.dropOnly("bar").tp  // Prints: Opt(foo)
Source
__.scala
inline def dropVoid(using d: Any.Def.Void[A]): Opt[A]

Reversed void filter

Reversed void filter

Discards value if it is void, so the option itself becomes void

 val s : Stream[String]     =VOID

 var o : Opt[Stream[String]] = s

 o.tp  // Prints: Opt(Stream())

 o = o.dropVoid

 o.tp  // Prints: Opt(VOID)
Source
__.scala
inline def filter(f: A => Boolean): Opt[A]

Legacy filter

Legacy filter

Discards value if it does not pass given filter function

Note: take is usually used instead.

Source
__.scala
inline def foldAs[B](v: => B)(f: A => B): B

Convert or default

Convert or default

This is a synthetic operation:

val o: Opt[String]=VOID

o.foldAs(0)(_.length)

// same as

o.map(_.length) or 0

foldAs is more readable if mapping code is substantial:

o.foldAs(0){ v =>
  var l = v.length
  if     (l > 1000) l = 1000
  else if(l < 0)    l = 0
  l
}
Source
__.scala
inline def fornil[U](f: => U): Opt[A]

Process nonexistent value

Process nonexistent value

Executes given function if option is void

Returns option itself

Source
__.scala
inline def forval[U](f: A => U): Opt[A]

Process option value

Process option value

Executes given function with option value

Does nothing if option is void

Returns option itself

Source
__.scala
inline def get: A

Get value

Get value

Returns value or fails if option is void

Note: This method is not widely used except for debugging and examples. or is the main way to resolve value

Source
__.scala
inline def isEmpty: Boolean

Void check

Void check

Returns true if value is void and false otherwise

Note: This operation is implicitly available for all types. It is explicit here to indicate efficient implementation.

Source
__.scala
inline def map[B](f: A => B)(using s: Specialized[B]): s.Opt

Convert value

Convert value

Creates new option with value converted by the given function

Void option allways yeilds void option

   "Abc".?.map(_.length)  // Prints: Int.Opt(3)

Note. Operation returns specialized options for primitive values

Source
__.scala
inline def mapIf(f: A => Boolean, m: A => A): Opt[A]
Source
__.scala
inline def mapOpt[B,OPT<:Any.Opt[B]](f: A => OPT)(using s: Specialized[B]): s.Opt

Optional map

Optional map

Creates new option with value converted by optional function.

Void option allways yeilds void option

"a"  .?.mapOpt(s => if(s.length > 2) s.toUpperCase else VOID).tp      // Prints Opt(VOID)
"abc".?.mapOpt(s => if(s.length > 2) s.toUpperCase else VOID).tp      // Prints Opt(ABC)

"a"  .?.mapOpt(s => if(s.length > 2) s.length else VOID).tp           // Prints Int.Opt(VOID)
"abc".?.mapOpt(s => if(s.length > 2) s.length else VOID).tp           // Prints Int.Opt(3)
"abc".?.mapOpt{
  case s if s.length > 2 => s.toUpperCase
  case _                 => VOID
}.tp

Note. In case of matching, the last default case must be given, so it becomes regular (not a partial) function

Source
__.scala
inline def mix[B,C](o: Any.Opt[B], f: (A, B) => C)(using s: Specialized[C]): s.Opt

Mix two option values

Mix two option values

If either option is void, the void option is returned

Otherwise, the given function is applied with both values, resulting in a valued option

 val io: Int.Opt = 4

 var so: Opt[String]=VOID


 so.mix(io, _ * _).tp  // Prints Opt(VOID)

 so = "abc_"

 so.mix(io, _ * _).tp // Prints Opt(abc_abc_abc_abc_)
Source
__.scala
inline def nonEmpty: Boolean

Not void check

Not void check

Returns true if value is not void and false otherwise

Note: This operation is implicitly available for all types. It is explicit here to indicate efficient implementation.

Source
__.scala
inline def or(default: => A): A

Value or default

Value or default

Returns option value, or if option is void, given default value

var o : Opt[String] = "foo"

(o or "bar").tp     // Prints foo

o=VOID

(o or "bar").tp     // Prints bar
Source
__.scala
inline def orOpt(that: => Opt[A]): Opt[A]

Default option

Default option

Returns this option if it is not void or given option otherwise

var o  : Opt[String] = "foo"
var o2 : Opt[String] = "bar"

(o orOpt o2).tp     // Prints Opt(foo)

o=VOID

(o orOpt o2).tp     // Prints Opt(bar)
Source
__.scala
inline def process[U,W](f: A => U, fNil: => W): Opt[A]

Process value or no value

Process value or no value

Executes given function with option value or second given function if option is void

Returns option itself

Source
__.scala
inline def raw(using sp: Specialized.Primitive[A]): s.Opt

Make specialized option

Make specialized option

Converts this option to specialized on primitive type

The operation will not compile if converting to a reference type

 var o  : Opt[Int] = 12
 var io : Int.Opt  = o.raw
Source
__.scala
inline def stream: Stream[A]

Stream

Stream

Returns single value stream or empty stream, if option is void

Source
__.scala
inline def take(f: A => Boolean): Opt[A]

Filter

Filter

Discards value if it does not satisfy given predicate

 val o : Opt[String] = "foo"

 o.take(_.length < 2).tp  // Prints: Opt(VOID)

 o.take(_.length > 2).tp  // Prints: Opt(foo)
Source
__.scala
inline def takeOnly(v: A): Opt[A]

Value filter

Value filter

Discards value if it is not equal to given value

 val o : Opt[String] = "foo"

 o.takeOnly("foo").tp  // Prints: Opt(foo)

 o.takeOnly("bar").tp  // Prints: Opt(VOID)
Source
__.scala
inline def takeType[B](using t: scala.reflect.ClassTag[B]): Opt[B]

Value filter and type converter

Value filter and type converter

Discards value if it does not belong to the given type

Note, the result is mapped to the given type

  val o: Opt[Any] = "1"

  println(o.takeType[String]) // Prints: Opt(1)

  println(o.takeType[Int])    // Prints: \/

Note: If Opt is empty, it is returned as is

Source
__.scala
inline def toJava: java.util.Optional[A]

Make Java Optional

Make Java Optional

Converts this option to java.util.Optional

Source
__.scala
inline def toScala: scala.Option[A]

Make Scala Option

Make Scala Option

Converts this option to scala.Option

Source
__.scala