Skip to content

Commit

Permalink
Revert "Add Opt.when method"
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak authored Apr 16, 2024
1 parent 28ca4c0 commit e67698c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 29 deletions.
19 changes: 2 additions & 17 deletions core/src/main/scala/com/avsystem/commons/misc/Opt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ object Opt {
def foreach[U](f: A => U): Unit = self filter p foreach f
def withFilter(q: A => Boolean): WithFilter[A] = new WithFilter[A](self, x => p(x) && q(x))
}

final class LazyOptOps[A](private val opt: () => Opt[A]) extends AnyVal {
/** When a given condition is true, evaluates the `opt` argument and returns it.
* When the condition is false, `opt` is not evaluated and `Opt.Empty` is
* returned.
*/
def when(cond: Boolean): Opt[A] = if (cond) opt() else Opt.Empty
/** Unless a given condition is true, this will evaluate the `opt` argument and
* return it. Otherwise, `opt` is not evaluated and `Opt.Empty` is returned.
*/
@inline def unless(cond: Boolean): Opt[A] = when(!cond)
}

implicit def lazyOptOps[A](opt: => Opt[A]): LazyOptOps[A] = new LazyOptOps(() => opt)
}

/**
Expand All @@ -55,8 +41,7 @@ object Opt {
* Please be aware of that tradeoff.
*/
final class Opt[+A] private(private val rawValue: Any) extends AnyVal with OptBase[A] with Serializable {

import Opt.*
import Opt._

private def value: A = rawValue.asInstanceOf[A]

Expand Down Expand Up @@ -155,7 +140,7 @@ final class Opt[+A] private(private val rawValue: Any) extends AnyVal with OptBa
if (isEmpty) Iterator.empty else Iterator.single(value)

@inline def toList: List[A] =
if (isEmpty) List() else value :: Nil
if (isEmpty) List() else new ::(value, Nil)

@inline def toRight[X](left: => X): Either[X, A] =
if (isEmpty) Left(left) else Right(value)
Expand Down
12 changes: 0 additions & 12 deletions core/src/test/scala/com/avsystem/commons/misc/OptTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,4 @@ class OptTest extends AnyFunSuite {
val seq: Seq[Int] = Opt(5).mapOr(Nil, i => 0 until i)
assert(seq == (0 until 5))
}

test("Opt.{when, unless}") {
val opt = Opt(42)

assert(opt.when(true) == opt)
assert(opt.when(false) == Opt.Empty)
assert(Opt(fail("Parameter should not be evaluated")).when(false) == Opt.Empty)

assert(opt.unless(false) == opt)
assert(opt.unless(true) == Opt.Empty)
assert(Opt(fail("Parameter should not be evaluated")).unless(true) == Opt.Empty)
}
}

0 comments on commit e67698c

Please sign in to comment.