Skip to content

Commit 8e87613

Browse files
committed
Trial: Make implicit resolution block scoped
So far, nesting was just one of several criteria for selecting a best implicit. What would happen if nesting was most significant? I.e. inner implicits would always win over outer ones? This has the potential to simplify the rules, gain effiviency, and solve the local consistency problem since we can disambiguate implicits using a local definition (see implicit-disambiguation.scala as a test case).
1 parent f4d02de commit 8e87613

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,7 @@ trait Implicits { self: Typer =>
11481148
*/
11491149
def compareCandidate(prev: SearchSuccess, ref: TermRef, level: Int): Int =
11501150
if (prev.ref eq ref) 0
1151+
else if (prev.level != level) prev.level - level
11511152
else nestedContext().test(implicit ctx => compare(prev.ref, ref, prev.level, level))
11521153

11531154
/** If `alt1` is also a search success, try to disambiguate as follows:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
B
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
abstract class A {
2+
def show: String
3+
}
4+
class B extends A {
5+
def show = "B"
6+
}
7+
class C extends A {
8+
def show = "C"
9+
}
10+
object M {
11+
def f given B, C : String = {
12+
implied a for A = infer[B]
13+
infer[A].show
14+
}
15+
}
16+
object Test extends App {
17+
implied b for B
18+
implied c for C
19+
println(M.f)
20+
}

0 commit comments

Comments
 (0)