You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The name of a given can be left out. So the definitions
41
42
of the last section can also be expressed like this:
43
+
42
44
```scala
43
45
givenOrd[Int] with
44
46
...
45
47
given [T](usingOrd[T]):Ord[List[T]] with
46
48
...
47
49
```
50
+
48
51
If the name of a given is missing, the compiler will synthesize a name from
49
52
the implemented type(s).
50
53
51
54
**Note** The name synthesized by the compiler is chosen to be readable and reasonably concise. For instance, the two instances above would get the names:
55
+
52
56
```scala
53
57
given_Ord_Int
54
58
given_Ord_List_T
55
59
```
60
+
56
61
The precise rules for synthesizing names are found [here](./relationship-implicits.html#anonymous-given-instances). These rules do not guarantee absence of name conflicts between
57
62
given instances of types that are "too similar". To avoid conflicts one can
58
63
use named instances.
@@ -62,15 +67,18 @@ use named instances.
62
67
## Alias Givens
63
68
64
69
An alias can be used to define a given instance that is equal to some expression. E.g.:
70
+
65
71
```scala
66
72
givenglobal:ExecutionContext=ForkJoinPool()
67
73
```
74
+
68
75
This creates a given `global` of type `ExecutionContext` that resolves to the right
69
76
hand side `ForkJoinPool()`.
70
77
The first time `global` is accessed, a new `ForkJoinPool` is created, which is then
71
78
returned for this and all subsequent accesses to `global`. This operation is thread-safe.
72
79
73
80
Alias givens can be anonymous as well, e.g.
81
+
74
82
```scala
75
83
givenPosition= enclosingTree.position
76
84
given (usingconfig: Config):Factory=MemoizingFactory(config)
@@ -83,11 +91,13 @@ but it can only implement a single type.
83
91
84
92
Given aliases can have the `inline` and `transparent` modifiers.
// code producing a value of a subtype of Annotations
89
98
}
90
99
```
100
+
91
101
Since `mkAnnotations` is `transparent`, the type of an application is the type of its right hand side, which can be a proper subtype of the declared result type `Annotations[A, T]`.
92
102
93
103
## Pattern-Bound Given Instances
@@ -100,14 +110,17 @@ for given Context <- applicationContexts do
100
110
pair match
101
111
case (ctx @givenContext, y) => ...
102
112
```
113
+
103
114
In the first fragment above, anonymous given instances for class `Context` are established by enumerating over `applicationContexts`. In the second fragment, a given `Context`
104
115
instance named `ctx` is established by matching against the first half of the `pair` selector.
105
116
106
117
In each case, a pattern-bound given instance consists of `given` and a type `T`. The pattern matches exactly the same selectors as the type ascription pattern `_: T`.
107
118
108
119
## Negated Givens
109
120
110
-
Scala 2's somewhat puzzling behavior with respect to ambiguity has been exploited to implement the analogue of a "negated" search in implicit resolution, where a query Q1 fails if some other query Q2 succeeds and Q1 succeeds if Q2 fails. With the new cleaned up behavior these techniques no longer work. But the new special type `scala.util.NotGiven` now implements negation directly.
121
+
Scala 2's somewhat puzzling behavior with respect to ambiguity has been exploited to implement the analogue of a "negated" search in implicit resolution,
122
+
where a query Q1 fails if some other query Q2 succeeds and Q1 succeeds if Q2 fails. With the new cleaned up behavior these techniques no longer work.
123
+
But the new special type `scala.util.NotGiven` now implements negation directly.
111
124
112
125
For any query type `Q`, `NotGiven[Q]` succeeds if and only if the implicit
113
126
search for `Q` fails, for example:
@@ -124,8 +137,8 @@ object Foo:
124
137
125
138
@main deftest() =
126
139
givenTagged[Int] with {}
127
-
assert(implicitly[Foo[Int]].value) // fooTagged is found
128
-
assert(!implicitly[Foo[String]].value) // fooNotTagged is found
140
+
assert(summon[Foo[Int]].value) // fooTagged is found
141
+
assert(!summon[Foo[String]].value) // fooNotTagged is found
129
142
```
130
143
131
144
## Given Instance Initialization
@@ -152,7 +165,7 @@ A given instance starts with the reserved word `given` and an optional _signatur
152
165
defines a name and/or parameters for the instance. It is followed by `:`. There are three kinds
153
166
of given instances:
154
167
155
-
- A _structural instance_ contains one or more types or constructor applications, followed by `with` and a template body
156
-
that contains member definitions of the instance.
157
-
- An _alias instance_ contains a type, followed by `=` and a right hand side expression.
158
-
- An _abstract instance_ contains just the type, which is not followed by anything.
168
+
- A _structural instance_ contains one or more types or constructor applications,
169
+
followed by `with` and a template body that contains member definitions of the instance.
170
+
- An _alias instance_ contains a type, followed by `=` and a right hand side expression.
171
+
- An _abstract instance_ contains just the type, which is not followed by anything.
0 commit comments