1
1
package com .avsystem .commons
2
2
3
3
import com .avsystem .commons .concurrent .RunNowEC
4
- import com .avsystem .commons .misc ._
5
-
4
+ import com .avsystem .commons .misc .*
6
5
import scala .annotation .{nowarn , tailrec }
7
6
import scala .collection .{AbstractIterator , BuildFrom , Factory , mutable }
8
7
9
8
trait SharedExtensions {
10
9
11
- import com .avsystem .commons .SharedExtensionsUtils ._
10
+ import com .avsystem .commons .SharedExtensionsUtils .*
12
11
13
12
implicit def universalOps [A ](a : A ): UniversalOps [A ] = new UniversalOps (a)
14
13
@@ -26,6 +25,8 @@ trait SharedExtensions {
26
25
27
26
implicit def futureCompanionOps (fut : Future .type ): FutureCompanionOps .type = FutureCompanionOps
28
27
28
+ implicit def lazyOptOps [A ](opt : => Opt [A ]): LazyOptOps [A ] = new LazyOptOps (() => opt)
29
+
29
30
implicit def optionOps [A ](option : Option [A ]): OptionOps [A ] = new OptionOps (option)
30
31
31
32
implicit def tryOps [A ](tr : Try [A ]): TryOps [A ] = new TryOps (tr)
@@ -188,6 +189,18 @@ object SharedExtensionsUtils extends SharedExtensions {
188
189
}
189
190
}
190
191
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
+
191
204
class NullableOps [A >: Null ](private val a : A ) extends AnyVal {
192
205
def optRef : OptRef [A ] = OptRef (a)
193
206
}
@@ -502,7 +515,7 @@ object SharedExtensionsUtils extends SharedExtensions {
502
515
503
516
class PartialFunctionOps [A , B ](private val pf : PartialFunction [A , B ]) extends AnyVal {
504
517
505
- import PartialFunctionOps ._
518
+ import PartialFunctionOps .*
506
519
507
520
/**
508
521
* The same thing as `orElse` but with arguments flipped.
@@ -638,7 +651,7 @@ object SharedExtensionsUtils extends SharedExtensions {
638
651
639
652
class MapOps [M [X , Y ] <: BMap [X , Y ], K , V ](private val map : M [K , V ]) extends AnyVal {
640
653
641
- import MapOps ._
654
+ import MapOps .*
642
655
643
656
def getOpt (key : K ): Opt [V ] = map.get(key).toOpt
644
657
0 commit comments