Opt

object Opt extends _extension
Source
__.scala
class _givens
class Object
trait Matchable
class Any

Def

Local

def apply[A](v: A): Opt[A]
Source
__.scala
inline def option[A](v: Option[A]): Opt[A]
Source
__.scala
inline def optional[A](v: Optional[A]): Opt[A]
Source
__.scala
@targetName("getVoid")
def void[A]: Opt[A]

Get void instance

Get void instance

Source
__.scala

Inherited inherited

@targetName("stream")
def ~[A]: ~[A]

Stream

Stream

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

Inherited from
_extension
Source
_extension.scala
def collect[A](f: 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 this is empty, it is returned as is

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

Inherited from
_extension
Source
_extension.scala
inline def contains[A](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
Inherited from
_extension
Source
_extension.scala
inline def default[A](inline 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 = \/

   o.default("bar").tp // Prints: Opt(bar)
Inherited from
_extension
Source
_extension.scala
inline def drop[A](inline 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(\/)

 o.drop(_.length > 3).tp  // Prints: Opt(foo)
Inherited from
_extension
Source
_extension.scala
inline def dropOnly[A](inline 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(\/)

 o.dropOnly("bar").tp  // Prints: Opt(foo)
Inherited from
_extension
Source
_extension.scala
inline def dropVoid[A](using inline t: Info.Tag.Void[A]): Opt[A]

Reversed void filter

Reversed void filter

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

 val s : ~[String]      = \/

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

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

 o = o.dropVoid

 o.tp  // Prints: Opt(\/)
Inherited from
_extension
Source
_extension.scala
inline def filter[A](inline 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.

Inherited from
_extension
Source
_extension.scala
inline def foldAs[A](inline v: => B)(inline f: A => B): B
Inherited from
_extension
Source
_extension.scala
inline def fornil[A](inline f: => U): Opt[A]

Process nonexistent value

Process nonexistent value

Executes given function if option is void

Returns option itself

Inherited from
_extension
Source
_extension.scala
inline def forval[A](inline 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

Inherited from
_extension
Source
_extension.scala
def get[A]: 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

Inherited from
_extension
Source
_extension.scala
@targetName("is_Void")
inline def isEmpty[A]: 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.

Inherited from
_extension
Source
_extension.scala
inline def map[A, T, OPT <: Any[T]](inline f: A => B)(using inline s: Shape.OfOpt.Tag[B, OPT]): 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

Inherited from
_extension
Source
_extension.scala
@targetName("map_Opt")
inline def map_?[A, T](inline f: A => OPT)(using inline s: Shape.OfOpt.Tag[T, OPT]): OPT

Optional map

Optional map

Creates new option with value converted by optional function.

Void option allways yeilds void option

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

"a".?  .map_?(s => if(s.length > 2) s.length else Int.Opt.\/).tp // Prints Int.Opt(\/)
"abc".?.map_?(s => if(s.length > 2) s.length else Int.Opt.\/).tp // Prints Int.Opt(3)

Note: The void option type has to be given explicitly

"abc".?.map_?{
  case s if s.length > 2 => s.toUpperCase
  case _                 => \/
}.tp

Note. In case matching the last default case must be given, so it becomes regular, not partial function, which is slower

Inherited from
_extension
Source
_extension.scala
inline def mix[A, B, C](inline o: Any[B], inline f: (A, B) => C)(using inline s: Shape.OfOpt.Tag[C, OPT]): 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] = \/


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

 so = "abc_"

 so.mix(io, _ * _).tp // Prints Opt(abc_abc_abc_abc_)
Inherited from
_extension
Source
_extension.scala
@targetName("not_Void")
inline def nonEmpty[A]: 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.

Inherited from
_extension
Source
_extension.scala
inline def or[A](inline 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 = \/

(o or "bar").tp     // Prints bar
Inherited from
_extension
Source
_extension.scala
@targetName("or_Opt")
inline def or_?[A](inline 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 or_? o2).tp     // Prints Opt(foo)

 o = \/

 (o or_? o2).tp     // Prints Opt(bar)
Inherited from
_extension
Source
_extension.scala
inline def process[A](inline f: A => U, inline 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

Inherited from
_extension
Source
_extension.scala
inline def raw[A, OPT <: Raw[A]](using inline s: Shape.OfOpt.Tag.Raw[A, OPT]): OPT

To specialized option

To specialized option

Converts this option to specialized on primitive type

The operation will not compile if convertin to reference type

 var o  : Opt[Int] = 12
 var io : Int.Opt  = o.ref
Inherited from
_extension
Source
_extension.scala
inline def take[A](inline 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(\/)

 o.take(_.length > 2).tp  // Prints: Opt(foo)
Inherited from
_extension
Source
_extension.scala
inline def takeOnly[A](inline 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(\/)
Inherited from
_extension
Source
_extension.scala
inline def takeType[A](using inline t: 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 this is empty, it is returned as is

Inherited from
_extension
Source
_extension.scala
inline def toJava[A]: Optional[A]

To Java Optional

To Java Optional

Converts this option to java.util.Optional

Inherited from
_extension
Source
_extension.scala
inline def toScala[A]: Option[A]

To Scala Option

To Scala Option

Converts this option to scala.Option

Inherited from
_extension
Source
_extension.scala