Skip to content

Commit

Permalink
Also allow structural dispatch on Dynamic
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
odersky committed Mar 15, 2023
1 parent 8509185 commit 2808f3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Dynamic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ trait Dynamic {
val vargss = termArgss(tree)

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

// ($qual: Selectable).$selectorName("$name")
val base =
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/i17100a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

import scala.language.dynamics
trait Sel extends Dynamic

extension (s: Sel)
def selectDynamic(name: String) = ???

val sel = new Sel {}
val foo = sel.foo
val sel2 = (new Sel {}).asInstanceOf[Sel{ def foo: String }]
val foo2 = sel2.foo

0 comments on commit 2808f3d

Please sign in to comment.