From 421591fc792480033e28714bb2ecc60c32923d9b Mon Sep 17 00:00:00 2001 From: Flavio Brasil Date: Sat, 11 Mar 2023 14:08:13 -0800 Subject: [PATCH] optimization: avoid NotGiven allocations --- library/src/scala/util/NotGiven.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/src/scala/util/NotGiven.scala b/library/src/scala/util/NotGiven.scala index 99cc903d4426..973e709042cb 100644 --- a/library/src/scala/util/NotGiven.scala +++ b/library/src/scala/util/NotGiven.scala @@ -31,11 +31,13 @@ trait LowPriorityNotGiven { } object NotGiven extends LowPriorityNotGiven { + private val cachedValue = new NotGiven[Nothing]() + /** A value of type `NotGiven` to signal a successful search for `NotGiven[C]` (i.e. a failing * search for `C`). A reference to this value will be explicitly constructed by Dotty's * implicit search algorithm */ - def value: NotGiven[Nothing] = new NotGiven[Nothing]() + def value: NotGiven[Nothing] = cachedValue /** One of two ambiguous methods used to emulate negation in Scala 2 */ given amb1[T](using ev: T): NotGiven[T] = ???