Skip to content

Commit 421591f

Browse files
committed
optimization: avoid NotGiven allocations
1 parent 528d931 commit 421591f

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

library/src/scala/util/NotGiven.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ trait LowPriorityNotGiven {
3131
}
3232
object NotGiven extends LowPriorityNotGiven {
3333

34+
private val cachedValue = new NotGiven[Nothing]()
35+
3436
/** A value of type `NotGiven` to signal a successful search for `NotGiven[C]` (i.e. a failing
3537
* search for `C`). A reference to this value will be explicitly constructed by Dotty's
3638
* implicit search algorithm
3739
*/
38-
def value: NotGiven[Nothing] = new NotGiven[Nothing]()
40+
def value: NotGiven[Nothing] = cachedValue
3941

4042
/** One of two ambiguous methods used to emulate negation in Scala 2 */
4143
given amb1[T](using ev: T): NotGiven[T] = ???

0 commit comments

Comments
 (0)