Skip to content

Commit cc832f8

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 cd1c914 commit cc832f8

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
@@ -1101,6 +1101,7 @@ trait Implicits { self: Typer =>
11011101
*/
11021102
def compareCandidate(prev: SearchSuccess, ref: TermRef, level: Int): Int =
11031103
if (prev.ref eq ref) 0
1104+
else if (prev.level != level) prev.level - level
11041105
else nestedContext().test(implicit ctx => compare(prev.ref, ref, prev.level, level))
11051106

11061107
/** 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)