小编hug*_*eat的帖子

Scala动态字段名为x

只需实现一个Dynamic对象(使用2.10.0-M3):

import language.dynamics
object D extends Dynamic {
    def selectDynamic( field : String ) = Symbol( field )
}
Run Code Online (Sandbox Code Playgroud)

以下工作正常并且符合预期

object DynamicTest extends App {
    println( D.a )
}
Run Code Online (Sandbox Code Playgroud)

印刷'a

但是,如果我试试这个:

object DynamicTest extends App {
    println( D.x )
}
Run Code Online (Sandbox Code Playgroud)

我得到了令人讨厌的错误:

[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 …
Run Code Online (Sandbox Code Playgroud)

scala scala-2.10

5
推荐指数
1
解决办法
433
查看次数

标签 统计

scala ×1

scala-2.10 ×1