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
Copy file name to clipboardExpand all lines: docs/_docs/reference/changed-features/implicit-resolution.md
+29-1
Original file line number
Diff line number
Diff line change
@@ -163,8 +163,36 @@ The new rules are as follows: An implicit `a` defined in `A` is more specific th
163
163
164
164
Condition (*) is new. It is necessary to ensure that the defined relation is transitive.
165
165
166
+
[//]: # todo: expand with precise rules
166
167
168
+
**9.** The following change is currently enabled in `-source future`:
167
169
170
+
Implicit resolution now avoids generating recursive givens that can lead to an infinite loop at runtime. Here is an example:
168
171
172
+
```scala
173
+
objectPrices {
174
+
opaquetypePrice=BigDecimal
175
+
176
+
objectPrice{
177
+
givenOrdering[Price] = summon[Ordering[BigDecimal]] // was error, now avoided
178
+
}
179
+
}
180
+
```
181
+
182
+
Previously, implicit resolution would resolve the `summon` to the given in `Price`, leading to an infinite loop (a warning was issued in that case). We now use the underlying given in `BigDecimal` instead. We achieve that by adding the following rule for implicit search:
183
+
184
+
- When doing an implicit search while checking the implementation of a `given` definition `G` of the form
185
+
```
186
+
given ... = ....
187
+
```
188
+
discard all search results that lead back to `G` or to a given with the same owner as `G` that comes later in the source than `G`.
189
+
190
+
The new behavior is currently enabled in `source.future` and will be enabled at the earliest in Scala 3.6. For earlier source versions, the behavior is as
191
+
follows:
192
+
193
+
- Scala 3.3: no change
194
+
- Scala 3.4: A warning is issued where the behavior will change in 3.future.
195
+
- Scala 3.5: An error is issued where the behavior will change in 3.future.
196
+
197
+
Old-style implicit definitions are unaffected by this change.
Implicit resolution now avoids generating recursive givens that can lead to an infinite loop at runtime. Here is an example:
9
+
10
+
```scala
11
+
objectPrices {
12
+
opaquetypePrice=BigDecimal
13
+
14
+
objectPrice{
15
+
givenOrdering[Price] = summon[Ordering[BigDecimal]] // was error, now avoided
16
+
}
17
+
}
18
+
```
19
+
20
+
Previously, implicit resolution would resolve the `summon` to the given in `Price`, leading to an infinite loop (a warning was issued in that case). We now use the underlying given in `BigDecimal` instead. We achieve that by adding the following rule for implicit search:
21
+
22
+
- When doing an implicit search while checking the implementation of a `given` definition `G` of the form
23
+
```
24
+
given ... = ....
25
+
```
26
+
discard all search results that lead back to `G` or to a given with the same owner as `G` that comes later in the source than `G`.
27
+
28
+
The new behavior is enabled with the `experimental.givenLoopPrevention` language import. If no such import or setting is given, a warning is issued where the behavior would change under that import (for source version 3.4 and later).
29
+
30
+
Old-style implicit definitions are unaffected by this change.
0 commit comments