Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selectDynamic and applyDynamic to be extension methods #17106

Merged
merged 3 commits into from
Mar 17, 2023

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Mar 15, 2023

Allow selectDynamic and applyDynamic to be extension methods when dispatching structurally.

Fixes #17100

@@ -186,7 +186,7 @@ trait Dynamic {
// ($qual: Selectable).$selectorName("$name")
val base =
untpd.Apply(
untpd.TypedSplice(selectable.select(selectorName)).withSpan(fun.span),
untpd.Select(untpd.TypedSplice(selectable), selectorName).withSpan(fun.span),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will indeed allow extension methods, but won't it also allow all sorts of other things? Like recursive structural lookups and insertion of implicit conversions? That seems too much (especially the recursive structural lookup).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will allow implicit conversions, and that's intentional. It will allow recursive structural lookup if selectDynamic is defined in the structural type. But I think that's a weird corner case, and I also see nothing wrong to support it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: That should have been applyDynamic.

@odersky odersky changed the title Allow selectDynamic and applyDynamic to be an extension methods Allow selectDynamic and applyDynamic to be extension methods Mar 15, 2023
Allow selectDynamic and applyDynamic to be an extension methods when dispatching structurally.

Fixes scala#17100
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`.
@prolativ
Copy link
Contributor

@odersky @sjrd the CI checks were successful. Can we proceed with this?

@odersky odersky merged commit 6c05e6c into scala:main Mar 17, 2023
@odersky odersky deleted the fix-17100 branch March 17, 2023 14:22
@Kordyjan Kordyjan added this to the 3.3.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

selectDynamic cannot be an extension method
4 participants