Skip to content

Commit

Permalink
Include puncture and only, with better implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Mar 5, 2024
1 parent b9aa769 commit 0d9d1bf
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/core/optional.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import fulminate.*

import language.experimental.captureChecking

import _root_.java.util as ju

object Unset:
override def toString(): String = ""

Expand All @@ -35,7 +37,8 @@ extension [ValueType](optional: Optional[ValueType])
if absent then value else optional.asInstanceOf[ValueType]

inline def vouch(using Unsafe): ValueType = or(throw Panic(msg"a value was vouched but was absent"))


def stdlib: ju.Optional[ValueType] = optional.lay(ju.Optional.empty[ValueType].nn)(ju.Optional.of(_).nn)
def presume(using default: Default[ValueType]): ValueType = or(default())
def option: Option[ValueType] = if absent then None else Some(vouch(using Unsafe))

Expand Down Expand Up @@ -72,5 +75,14 @@ extension [ValueType](option: Option[ValueType])
inline def optional: Unset.type | ValueType = option.getOrElse(Unset)
def presume(using default: Default[ValueType]) = option.getOrElse(default())

extension [ValueType](value: ValueType)
def puncture(point: ValueType): Optional[ValueType] = if value == point then Unset else value

def only[ValueType2](partial: PartialFunction[ValueType, ValueType2]): Optional[ValueType2] =
(partial.orElse { _ => Unset })(value)

extension [ValueType](java: ju.Optional[ValueType])
def optional: Optional[ValueType] = if java.isEmpty then Unset else java.get.nn

erased trait Unsafe
erased val Unsafe: Unsafe = compiletime.erasedValue

0 comments on commit 0d9d1bf

Please sign in to comment.