Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/scala.util/NotGiven

NotGiven

scala.util.NotGiven
See theNotGiven companion object
finalclassNotGiven[+T]

A special class used to implement negation in implicit search.

Consider the problem of using impliciti1 for a query typeD if an implicit for some other classC is available, and using an impliciti2 if no implicit value of typeC is available. If we do not want to prioritizei1 andi2 by putting them in different traits we can instead define the following:

given i1: D(using ev: C) = ... given i2: D(using ev: NotGiven[C]) = ...

NotGiven is treated specially in implicit search, similar to the way logical negation is treated in Prolog: The implicit search forNotGiven[C] succeeds if and only if the implicit search forC fails.

In Scala 2 this form of negation can be simulated by setting up a conditional ambiguous implicit and an unconditional fallback, the way it is done with thedefault,amb1 andamb2 methods below. Due to the way these two methods are defined,NotGiven is also usable from Scala 2.

In Dotty, ambiguity is a global error, and therefore cannot be used to implement negation. Instead,NotGiven is treated natively in implicit search.

Attributes

Companion
object
Source
NotGiven.scala
Graph
Supertypes
classObject
traitMatchable
classAny
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp