Skip to content

Dynamic doesn't compile with Typeclass #22158

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

Closed
sequencer opened this issue Dec 6, 2024 · 3 comments
Closed

Dynamic doesn't compile with Typeclass #22158

sequencer opened this issue Dec 6, 2024 · 3 comments

Comments

@sequencer
Copy link

sequencer commented Dec 6, 2024

Compiler version

3.5.2

Minimized code

import scala.language.dynamics

trait DynamicHello[E] extends Dynamic:
  extension (ref: E)
    def selectDynamic(
      field: String
    ): Any

given DynamicHello[String] with
  extension (ref: String)
    override def selectDynamic(field: String) = s"Hello $field"

// Doesn't compile with error:
// value hello is not a member of String [15:6]
def badHello(str: String) =
  str.hello

// compile, and prove `DynamicHello[String]` does give to `String`
def goodHello(str: String) =
  str.selectDynamic("hello")

Output

[info] compiling 1 Scala source to xxx ...
[error] -- [E008] Not Found Error: Example.scala:15:6
[error] 18 |  str.hello
[error]    |  ^^^^^^^^^
[error]    |  value hello is not a member of String
[error] one error found

May relate to #17106

@sequencer sequencer added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 6, 2024
@sjrd
Copy link
Member

sjrd commented Dec 6, 2024

As designed. The spec specifically forbids the Dynamic treatment after an implicit conversion, which also covers extension methods.

@sjrd sjrd closed this as not planned Won't fix, can't repro, duplicate, stale Dec 6, 2024
@sjrd sjrd added itype:invalid and removed itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 6, 2024
@sequencer
Copy link
Author

Hi, @sjrd can you point out the source of spec here? I think this issue is similar with #17106

@sjrd
Copy link
Member

sjrd commented Dec 18, 2024

https://www.scala-lang.org/files/archive/spec/3.4/06-expressions.html#dynamic-member-selection

Notice

assuming e's type conforms to scala.Dynamic

For it to conform to scala.Dynamic, it must be a true subtype. It cannot become a scala.Dynamic after an implicit conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants