Skip to content

Commit fa29ba5

Browse files
committed
Also allow structural dispatch on Dynamic
Given ```scala trait Sel extends Dynamic extension (s: Sel) def selectDynamic(name: String) = ??? ``` the following worked: ```scala val sel = new Sel {} val foo = sel.foo ``` but the following didn't: ```scala val sel2 = (new Sel {}).asInstanceOf[Sel{ def foo: String }] val foo2 = sel2.foo ``` The problem was that we recognized a structural dispatch and then required the qualifier to be an instance of `Selectable`. But in fact, `Dynamic` works just as well, and the mechanism is the same. It's just that `Dynamic` is less type safe then `Selectable`.
1 parent a3242e8 commit fa29ba5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ trait Dynamic {
181181
val vargss = termArgss(tree)
182182

183183
def structuralCall(selectorName: TermName, classOfs: => List[Tree]) = {
184-
val selectable = adapt(qual, defn.SelectableClass.typeRef)
184+
val selectable = adapt(qual, defn.SelectableClass.typeRef | defn.DynamicClass.typeRef)
185185

186186
// ($qual: Selectable).$selectorName("$name")
187187
val base =

tests/pos/i17100a.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
import scala.language.dynamics
3+
trait Sel extends Dynamic
4+
5+
extension (s: Sel)
6+
def selectDynamic(name: String) = ???
7+
8+
val sel = new Sel {}
9+
val foo = sel.foo
10+
val sel2 = (new Sel {}).asInstanceOf[Sel{ def foo: String }]
11+
val foo2 = sel2.foo
12+

0 commit comments

Comments
 (0)