Implicit conversion from any to dynamic

Why does the following not work? (Yes, I work with 2.9.0final and have included the option "-Xexperimental".)

implicit def any2Dynamic(a: Any) = new Dynamic { def applyDynamic(name: String)(args: Any*) = { println(a + name) } } "Say".hello // value hello is not a member of java.lang.String 

You can argue about how significant it is ... If it worked, as expected, what would happen in "Say".toInt : StringLike.toInt or (new Dynamic {...}).applyDynamic("toInt") ?

+8
scala dynamic-typing
source share
1 answer

The first compiler is looking for an implicit view from String => { def hello: ? } String => { def hello: ? } . This fails, so it checks if String <: Dynamic . They are not combined.

This dynamic application function has not been finalized - in Scala 2.9.0 it is experimental and can be changed. But I doubt that this will be included, since with that implied you are throwing all kinds of security out of the window. You will never get a compilation error for misspelled names or invalid argument types. What is your use case?

+9
source share

All Articles