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

selectDynamic/applyDynamic logic fails too eagerly #7059

Closed
scabug opened this issue Feb 3, 2013 · 6 comments
Closed

selectDynamic/applyDynamic logic fails too eagerly #7059

scabug opened this issue Feb 3, 2013 · 6 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Feb 3, 2013

This should work.

import scala.language.dynamics

class A extends Dynamic {
  def selectDynamic(method: String): B = new B(method)
}
class B(method: String) {
  def apply(args: Any*) = println("Called " + method)
}

object Test {
  def main(args: Array[String]): Unit = {
    val x = new A
    println(x.bippy("arg"))
  }
}

Instead, the compile fails:

% scalac3 ./a.scala 
./a.scala:13: error: value applyDynamic is not a member of A
error after rewriting to x.<applyDynamic: error>("bippy")
possible cause: maybe a wrong Dynamic method signature?
    println(x.bippy("arg"))
            ^
one error found

It does compile like this:

  println(x.bippy.apply("arg"))

But that renders it unsuitable for most plausible uses of Dynamic.

This is especially important because if this worked it would offer a way to achieve a more general form of overload than is currently possible.

@scabug
Copy link
Author

@scabug scabug commented Feb 3, 2013

@scabug
Copy link
Author

@scabug scabug commented Jul 10, 2013

@adriaanm said:
Unassigning and rescheduling to M6 as previous deadline was missed.

@scabug
Copy link
Author

@scabug scabug commented Dec 27, 2013

@xeno-by said:
Interestingly enough, if one changes selectDynamic to applyDynamic, everything works fine.

@scabug
Copy link
Author

@scabug scabug commented Dec 27, 2013

@xeno-by said:
One might say that this behavior is warranted by SIP-17: http://docs.scala-lang.org/sips/completed/type-dynamic.html. Here's a relevant excerpt:

  • If qual.sel is followed by a type argument list [Ts] (optionally) and an argument list (arg1, …, argn), where none of the arguments argi are named, and argn is not a sequence argument, i.e., of the shape e: _* (the argument list is required, though it may be empty), as in: qual.sel[Ts](arg1, …, argn), rewrite the expression to: qual.applyDynamic[Ts](“sel”)(arg1, …, argn)

  • If qual.sel is not followed by arguments or an assignment operator: qual.selectDynamic[Ts](“sel”).

If we follow the letter of the SIP, then x.bippy in x.bippy("arg") is followed by an argument list, hence it should get desugared into applyDynamic, not selectDynamic. Do you agree?

@scabug
Copy link
Author

@scabug scabug commented Dec 27, 2013

@paulp said:
I agree that's what the sip says, but as you know I do not generally find such documents particularly relevant. If there's a better way to do things then it's a bug in the sip. I see zero utility in intentionally generating a call to a method which can be seen not to exist and in so doing making it impossible to reach the method which does exist.

@scabug
Copy link
Author

@scabug scabug commented Dec 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants