Skip to content

Commit

Permalink
add when, unless extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak committed Apr 9, 2024
1 parent eccd36a commit cec85fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
23 changes: 18 additions & 5 deletions core/src/main/scala/com/avsystem/commons/SharedExtensions.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.avsystem.commons

import com.avsystem.commons.concurrent.RunNowEC
import com.avsystem.commons.misc._

import com.avsystem.commons.misc.*
import scala.annotation.{nowarn, tailrec}
import scala.collection.{AbstractIterator, BuildFrom, Factory, mutable}

trait SharedExtensions {

import com.avsystem.commons.SharedExtensionsUtils._
import com.avsystem.commons.SharedExtensionsUtils.*

implicit def universalOps[A](a: A): UniversalOps[A] = new UniversalOps(a)

Expand All @@ -26,6 +25,8 @@ trait SharedExtensions {

implicit def futureCompanionOps(fut: Future.type): FutureCompanionOps.type = FutureCompanionOps

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

implicit def optionOps[A](option: Option[A]): OptionOps[A] = new OptionOps(option)

implicit def tryOps[A](tr: Try[A]): TryOps[A] = new TryOps(tr)
Expand Down Expand Up @@ -188,6 +189,18 @@ object SharedExtensionsUtils extends SharedExtensions {
}
}

implicit 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)
}

class NullableOps[A >: Null](private val a: A) extends AnyVal {
def optRef: OptRef[A] = OptRef(a)
}
Expand Down Expand Up @@ -502,7 +515,7 @@ object SharedExtensionsUtils extends SharedExtensions {

class PartialFunctionOps[A, B](private val pf: PartialFunction[A, B]) extends AnyVal {

import PartialFunctionOps._
import PartialFunctionOps.*

/**
* The same thing as `orElse` but with arguments flipped.
Expand Down Expand Up @@ -638,7 +651,7 @@ object SharedExtensionsUtils extends SharedExtensions {

class MapOps[M[X, Y] <: BMap[X, Y], K, V](private val map: M[K, V]) extends AnyVal {

import MapOps._
import MapOps.*

def getOpt(key: K): Opt[V] = map.get(key).toOpt

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,17 @@ class SharedExtensionsTest extends AnyFunSuite with Matchers {
| abc
| abc""".stripMargin)
}

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 cec85fc

Please sign in to comment.