Implicit Conversion from Any to Dynamic
Why isn't the following working? (Yes, I am working with 2.9.0final and turned the "-Xexperimental" option on.)
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
One can argue about how meaningful this is... If this would work as expected what precedence will take place at "Say".toInt: Strin开发者_运维技巧gLike.toInt or (new Dynamic {...}).applyDynamic("toInt")?
The compiler first looks for an implicit view from String => { def hello: ? }. That fails, so it then checks if String <: Dynamic. These are not combined.
This dynamic apply feature has not been finalized -- in Scala 2.9.0 it is experimental, and subject to change. But I doubt this would be included, as with such an implicit, you throw all type safety out the window. You would never get a compile error for misspelled method names or incorrect argument types. What is your use case?
加载中,请稍侯......
精彩评论