Skip to content

Commit

Permalink
Rename unset to absent
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Dec 6, 2023
1 parent 3b3f2f6 commit 5bb0fe5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/core/conversions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object Unapply:

given maybe[MatchType, ResultType](using ext: Unapply[MatchType, ResultType])
: Unapply[Maybe[MatchType], ResultType] =
value => if value.unset then None else ext.unapply(value.asInstanceOf[MatchType])
value => if value.absent then None else ext.unapply(value.asInstanceOf[MatchType])

given [ResultType](using ext: Irrefutable[Text, ResultType]): Irrefutable[String, ResultType] = v =>
ext.unapply(v.tt)
Expand Down
18 changes: 9 additions & 9 deletions src/core/maybe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@ import fulminate.*
import language.experimental.captureChecking

object Unset:
override def toString(): String = "[unset]"
override def toString(): String = "[absent]"

type Maybe[ValueType] = Unset.type | ValueType

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

extension [ValueType](maybe: Maybe[ValueType])
inline def unset: Boolean = maybe == Unset
inline def absent: Boolean = maybe == Unset
inline def present: 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"))
inline def or(inline value: => ValueType): ValueType = if absent then value else maybe.asInstanceOf[ValueType]
inline def vouch(using Unsafe): ValueType = or(throw Mistake(msg"a value was vouched but was absent"))

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 option: Option[ValueType] = if absent then None else Some(vouch(using Unsafe))
def assume(using absentValue: CanThrow[UnsetValueError]): ValueType^{absentValue} = or(throw UnsetValueError())

inline def fm[ValueType2](inline alternative: => ValueType2)(inline fn: ValueType => ValueType2): ValueType2 =
if unset then alternative else fn(vouch(using Unsafe))
if absent then alternative else fn(vouch(using Unsafe))

inline def let[ValueType2](inline fn: ValueType => ValueType2): Maybe[ValueType2] =
if unset then Unset else fn(vouch(using Unsafe))
if absent then Unset else fn(vouch(using Unsafe))

extension [ValueType](iterable: Iterable[Maybe[ValueType]])
transparent inline def vouched: Iterable[ValueType] = iterable.filter(!_.unset).map(_.vouch(using Unsafe))
transparent inline def vouched: Iterable[ValueType] = iterable.filter(!_.absent).map(_.vouch(using Unsafe))

object Maybe:
inline def apply[ValueType](value: ValueType | Null): Maybe[ValueType] =
Expand Down
28 changes: 14 additions & 14 deletions src/test/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -217,52 +217,52 @@ object Tests extends Suite(t"Rudiments Tests"):
.assert(_ == (11, 4))

suite(t"Maybe tests"):
val unsetInt: Maybe[Int] = Unset
val absentInt: Maybe[Int] = Unset
val setInt: Maybe[Int] = 42

test(t"Check whether unset value is unset"):
unsetInt.unset
test(t"Check whether absent value is absent"):
absentInt.absent
.assert(_ == true)

test(t"Check thet set value is not unset"):
setInt.unset
test(t"Check thet set value is not absent"):
setInt.absent
.assert(_ == false)

test(t"Unsafely vouch a set value"):
val x: Int = setInt.vouch(using Unsafe)
x
.assert(_ == 42)

test(t"Provide an alternative for an unset value"):
unsetInt.or(1)
test(t"Provide an alternative for an absent value"):
absentInt.or(1)
.assert(_ == 1)

test(t"Provide an alternative for a set value"):
setInt.or(1)
.assert(_ == 42)

test(t"Presume a default value for an unset value"):
unsetInt.presume
test(t"Presume a default value for an absent value"):
absentInt.presume
.assert(_ == 0)

test(t"Convert an unset value to an Option"):
unsetInt.option
test(t"Convert an absent value to an Option"):
absentInt.option
.assert(_ == None)

test(t"Convert a set value to an Option"):
setInt.option
.assert(_ == Some(42))

test(t"Fold over a Maybe"):
unsetInt.fm(0)(_ + 1)
absentInt.fm(0)(_ + 1)
.assert(_ == 0)

test(t"Fold over a set Maybe"):
setInt.fm(0)(_ + 1)
.assert(_ == 43)

test(t"Map over an unset Maybe"):
unsetInt.let(_ + 1)
test(t"Map over an absent Maybe"):
absentInt.let(_ + 1)
.assert(_ == Unset)

test(t"Map over a set Maybe"):
Expand Down

0 comments on commit 5bb0fe5

Please sign in to comment.