Skip to content

Commit

Permalink
Inline some Maybe methods and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Nov 22, 2023
1 parent a227458 commit bb162e7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/core/collections.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extension [ValueType](seq: Iterable[ValueType])
transparent inline def mtwin: Iterable[(ValueType, ValueType)] = seq.map { x => (x, x) }
transparent inline def mtriple: Iterable[(ValueType, ValueType, ValueType)] = seq.map { x => (x, x, x) }


def indexBy[ValueType2](fn: ValueType -> ValueType2): Map[ValueType2, ValueType] throws DuplicateIndexError =
val map = seq.map: value =>
(fn(value), value)
Expand Down
1 change: 1 addition & 0 deletions src/core/default.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ object Default:
given [ElemType]: Default[Set[ElemType]](Set())
given [ElemType]: Default[Vector[ElemType]](Vector())

@capability
trait Default[+ValueType](default: ValueType):
def apply(): ValueType = default

Expand Down
36 changes: 16 additions & 20 deletions src/core/maybe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,28 @@ type Maybe[ValueType] = Unset.type | ValueType

case class UnsetValueError() extends Error(Message("the value was not set".tt))

extension [ValueType](maybe: Maybe[ValueType]^)
def unset: Boolean = maybe == Unset
def cast(using Unsafe): ValueType = maybe.asInstanceOf[ValueType]
def or(value: => ValueType): ValueType^{maybe, value} = if unset then value else cast(using Unsafe)
def presume(using default: => Default[ValueType]): ValueType^{maybe, default} = or(default())
extension [ValueType](maybe: Maybe[ValueType])
inline def unset: Boolean = maybe == Unset
inline def or(inline value: => ValueType): ValueType = if unset then value else maybe.asInstanceOf[ValueType]
inline def vouch(using Unsafe): ValueType = or(throw Mistake(msg"a value was vouched but was unset"))

def vouch(using Unsafe): ValueType^{maybe} =
or(throw Mistake(msg"a value was vouched as set but was unset"))
def presume(using default: Default[ValueType]): ValueType^{default} = or(default())
def option: Option[ValueType] = if unset then None else Some(vouch(using Unsafe))
def assume(using unsetValue: CanThrow[UnsetValueError]): ValueType^{unsetValue} = or(throw UnsetValueError())

def assume(using unsetValue: CanThrow[UnsetValueError]): ValueType^{maybe, unsetValue} =
or(throw UnsetValueError())


def option: Option[ValueType] = if unset then None else Some(cast(using Unsafe))
inline def fm[ValueType2](inline alternative: => ValueType2)(inline fn: ValueType => ValueType2): ValueType2 =
if unset then alternative else fn(vouch(using Unsafe))

def fm
[ValueType2](default: => ValueType2)(fn: ValueType => ValueType2)
: ValueType2^{default, fn} =
if unset then default else fn(cast(using Unsafe))
inline def mm[ValueType2](inline fn: ValueType => ValueType2): Maybe[ValueType2] =
if unset then Unset else fn(vouch(using Unsafe))

def mm[ValueType2](fn: ValueType => ValueType2): Maybe[ValueType2]^{fn} =
if unset then Unset else fn(cast(using Unsafe))
extension [ValueType](iterable: Iterable[Maybe[ValueType]])
transparent inline def vouched: Iterable[ValueType] = iterable.filter(!_.unset).map(_.vouch(using Unsafe))

object Maybe:
def apply[ValueType](value: ValueType | Null): Maybe[ValueType] = if value == null then Unset else value
inline def apply[ValueType](value: ValueType | Null): Maybe[ValueType] =
if value == null then Unset else value

extension [ValueType](option: Option[ValueType])
def maybe: Unset.type | ValueType = option.getOrElse(Unset)
inline def maybe: Unset.type | ValueType = option.getOrElse(Unset)
def presume(using default: Default[ValueType]) = option.getOrElse(default())

0 comments on commit bb162e7

Please sign in to comment.