Skip to content

Commit 0319c0b

Browse files
committed
Discuss explicit prioritization by owner
1 parent 67fc9a0 commit 0319c0b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

_posts/2024-08-01-given-priority-change-3.5.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,35 @@ This will output all parameters explicitly:
138138
...
139139
```
140140

141+
### Explicit Prioritization by Owner
142+
143+
One effective way to ensure that the most specific given instance is
144+
selected -— particularly useful when migrating libraries to Scala 3.5 -—
145+
is to leverage the inheritance rules as outlined in point 8 of [the
146+
language
147+
reference](https://docs.scala-lang.org/scala3/reference/changed-features/implicit-resolution.html):
148+
149+
```scala
150+
class General
151+
class Specific extends General
152+
153+
class LowPriority:
154+
given a:General()
155+
156+
object NormalPriority extends LowPriority:
157+
given b:Specific()
158+
159+
def run =
160+
import NormalPriority.given
161+
val x = summon[General]
162+
val _: Specific = x // <- b was picked
163+
```
164+
165+
The idea is to enforce prioritization through the inheritance
166+
hierarchies of classes that provide `given` instances. By importing the
167+
`given` instances from the object with the highest priority, you can
168+
control which instance is selected by the compiler.
169+
141170
### Outlook
142171

143172
We are considering adding `-rewrite` rules that automatically insert

0 commit comments

Comments
 (0)