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'

By Scalqa convention, method names, which return Opt type, are ended with '_?' instead of word 'Option'

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

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

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

Def

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

Stream

Stream

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

Inherited from
_methods
Source
_methods.scala
def collect[A](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 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
_methods
Source
_methods.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
_methods
Source
_methods.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
_methods
Source
_methods.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
_methods
Source
_methods.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
_methods
Source
_methods.scala
inline def dropVoid[A](using inline t: Given.VoidTag[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
_methods
Source
_methods.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
_methods
Source
_methods.scala
inline def foldAs[A](inline v: => B)(inline f: A => B): B
Inherited from
_methods
Source
_methods.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
_methods
Source
_methods.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
_methods
Source
_methods.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
_methods
Source
_methods.scala
@scala.annotation.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
_methods
Source
_methods.scala
inline def map[A, T, OPT <: Opt.AnyType[T]](inline f: A => B)(using inline s: Given.OptTag[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
_methods
Source
_methods.scala
@scala.annotation.targetName("map_Opt")
inline def map_?[A, T](inline f: A => OPT)(using inline s: Given.OptTag[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 Opt.void).tp         // Prints Opt(\/)
"abc".?.map_?(s => if(s.length > 2) s.toUpperCase else Opt.void).tp         // Prints Opt(ABC)
"a".?  .map_?[Opt[String]](s => if(s.length > 2) s.toUpperCase else \/).tp  // Prints Opt(\/)
"abc".?.map_?[Opt[String]](s => if(s.length > 2) s.toUpperCase else \/).tp  // Prints Opt(ABC)

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

Note: The void option type has to be given explicitly unless target option type is given explicitly

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

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

Inherited from
_methods
Source
_methods.scala
inline def mix[A, B, C](inline o: Opt.AnyType[B], inline f: (A, B) => C)(using inline s: Given.OptTag[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
_methods
Source
_methods.scala
@scala.annotation.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
_methods
Source
_methods.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
_methods
Source
_methods.scala
@scala.annotation.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
_methods
Source
_methods.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
_methods
Source
_methods.scala
inline def raw[A, OPT <: Opt.RawType[A]](using inline s: Given.OptTag.Raw[A, Opt._methods.OPT]): 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
Inherited from
_methods
Source
_methods.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
_methods
Source
_methods.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
_methods
Source
_methods.scala
inline def takeType[A](using inline 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 this is empty, it is returned as is

Inherited from
_methods
Source
_methods.scala
inline def toJava[A]: java.util.Optional[A]

Make Java Optional

Make Java Optional

Converts this option to java.util.Optional

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

Make Scala Option

Make Scala Option

Converts this option to scala.Option

Inherited from
_methods
Source
_methods.scala

Given

Inherited from
_givens
Source
_givens.scala
Inherited from
_givens
Source
_givens.scala
given givenDocTag[A](using val t: Given.DocTag[A]): Given.DocTag[Opt[A]]
Inherited from
_givens
Source
_givens.scala
Inherited from
_givens
Source
_givens.scala
given givenUsingOpt: Opt[A[B]]
Inherited from
_givens
Source
_givens.scala
given givenUsingOptNone: Opt[A[B]]
Inherited from
givenDefailts
Source
_givens.scala
Inherited from
_givens
Source
_givens.scala

Implicit

implicit inline def implicitFromBooleanOpt[A <: Raw.Boolean](inline v: Boolean.G.Opt[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromByteOpt[A <: Raw.Byte](inline v: Byte.G.Opt[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromCharOpt[A <: Raw.Char](inline v: Char.G.Opt[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromDoubleOpt[A <: Raw.Double](inline v: Double.G.Opt[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromFloatOpt[A <: Raw.Float](inline v: Float.G.Opt[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromFromValue[A](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitFromIntOpt[A <: Raw.Int](inline v: Int.G.Opt[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromJava[A](v: java.util.Optional[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromLongOpt[A <: Raw.Long](inline v: Long.G.Opt[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromResult[A](inline v: Result[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromScala[A](v: scala.Option[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromShortOpt[A <: Raw.Short](inline v: Short.G.Opt[A]): Opt[A]
Source
__.scala
implicit inline def implicitFromValue[A <: Raw.Boolean](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitFromValue[A <: Raw.Byte](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitFromValue[A <: Raw.Char](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitFromValue[A <: Raw.Short](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitFromValue[A <: Raw.Int](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitFromValue[A <: Raw.Long](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitFromValue[A <: Raw.Float](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitFromValue[A <: Raw.Double](inline v: A): Opt[A]
Source
__.scala
implicit inline def implicitRequest[A](inline v: \/): Opt[A]

General void instance request

General void instance request

It is possible to use general request \/ to get void instance of this type, thanks to this implicit conversion.

Source
__.scala
implicit inline def implicitToBoolean[A](inline v: Opt[A]): Boolean
Source
__.scala