Skip to content

Commit cec85fc

Browse files
committed
add when, unless extension methods
1 parent eccd36a commit cec85fc

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

core/src/main/scala/com/avsystem/commons/SharedExtensions.scala

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.avsystem.commons
22

33
import com.avsystem.commons.concurrent.RunNowEC
4-
import com.avsystem.commons.misc._
5-
4+
import com.avsystem.commons.misc.*
65
import scala.annotation.{nowarn, tailrec}
76
import scala.collection.{AbstractIterator, BuildFrom, Factory, mutable}
87

98
trait SharedExtensions {
109

11-
import com.avsystem.commons.SharedExtensionsUtils._
10+
import com.avsystem.commons.SharedExtensionsUtils.*
1211

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

@@ -26,6 +25,8 @@ trait SharedExtensions {
2625

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

28+
implicit def lazyOptOps[A](opt: => Opt[A]): LazyOptOps[A] = new LazyOptOps(() => opt)
29+
2930
implicit def optionOps[A](option: Option[A]): OptionOps[A] = new OptionOps(option)
3031

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

192+
implicit class LazyOptOps[A](private val opt: () => Opt[A]) extends AnyVal {
193+
/** When a given condition is true, evaluates the `opt` argument and returns it.
194+
* When the condition is false, `opt` is not evaluated and `Opt.Empty` is
195+
* returned.
196+
*/
197+
def when(cond: Boolean): Opt[A] = if (cond) opt() else Opt.Empty
198+
/** Unless a given condition is true, this will evaluate the `opt` argument and
199+
* return it. Otherwise, `opt` is not evaluated and `Opt.Empty` is returned.
200+
*/
201+
@inline def unless(cond: Boolean): Opt[A] = when(!cond)
202+
}
203+
191204
class NullableOps[A >: Null](private val a: A) extends AnyVal {
192205
def optRef: OptRef[A] = OptRef(a)
193206
}
@@ -502,7 +515,7 @@ object SharedExtensionsUtils extends SharedExtensions {
502515

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

505-
import PartialFunctionOps._
518+
import PartialFunctionOps.*
506519

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

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

641-
import MapOps._
654+
import MapOps.*
642655

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

core/src/test/scala/com/avsystem/commons/misc/SharedExtensionsTest.scala

+13
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,17 @@ class SharedExtensionsTest extends AnyFunSuite with Matchers {
206206
| abc
207207
| abc""".stripMargin)
208208
}
209+
210+
test("Opt.{when, unless}") {
211+
val opt = Opt(42)
212+
213+
assert(opt.when(true) == opt)
214+
assert(opt.when(false) == Opt.Empty)
215+
assert(Opt(fail("Parameter should not be evaluated")).when(false) == Opt.Empty)
216+
217+
assert(opt.unless(false) == opt)
218+
assert(opt.unless(true) == Opt.Empty)
219+
assert(Opt(fail("Parameter should not be evaluated")).unless(true) == Opt.Empty)
220+
}
221+
209222
}

0 commit comments

Comments
 (0)