diff --git a/src/core/conversions.scala b/src/core/conversions.scala index 8f436ff..9fe1aee 100644 --- a/src/core/conversions.scala +++ b/src/core/conversions.scala @@ -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) diff --git a/src/core/maybe.scala b/src/core/maybe.scala index 292e44f..e88e57c 100644 --- a/src/core/maybe.scala +++ b/src/core/maybe.scala @@ -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] = diff --git a/src/test/tests.scala b/src/test/tests.scala index b0525f7..3f6cca1 100644 --- a/src/test/tests.scala +++ b/src/test/tests.scala @@ -217,15 +217,15 @@ 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"): @@ -233,20 +233,20 @@ object Tests extends Suite(t"Rudiments Tests"): 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"): @@ -254,15 +254,15 @@ object Tests extends Suite(t"Rudiments Tests"): .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"):