Scala dynamic field named x

Simple implementation of a dynamic object (using 2.10.0-M3):

import language.dynamics
object D extends Dynamic {
    def selectDynamic( field : String ) = Symbol( field )
}

The following works fine and as expected

object DynamicTest extends App {
    println( D.a )
}

Print 'a

But if I try this:

object DynamicTest extends App {
    println( D.x )
}

I get nasty errors:

[error] DynProb.scala:7: type mismatch;
[error]  found   : D.type
[error]  required: ?{val x: ?}
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error]  both method any2Ensuring in object Predef of type [A](x: A)Ensuring[A]
[error]  and method any2ArrowAssoc in object Predef of type [A](x: A)ArrowAssoc[A]
[error]  are possible conversion functions from D.type to ?{val x: ?}
[error]     println( D.x )
[error]              ^
[error] one error found

Why is x so special? Am I doing something stupid?

+5
source share
1 answer

At the moment, I came up with a simple solution:

import language.dynamics
object D extends Dynamic {
    def selectDynamic( field : String ) = Symbol( field )
    def x = selectDynamic("x")
}
D.x

Returns expected

res2: Symbol = 'x

I would like to redefine the trait with the name CompleteDynamicin order to redefine this variable "x", but I could not at the moment.

0
source

All Articles