我在Scala邮件列表上看到了以下代码段:
scala> class DynamicImpl(x: AnyRef) extends Dynamic {
| def _select_(name: String): DynamicImpl = {
| new DynamicImpl(x.getClass.getMethod(name).invoke(x))
| }
| def _invoke_(name: String)(args: Any*) = {
| new DynamicImpl(x.getClass.getMethod(name, args.map(_.asInstanceOf[AnyRef].getClass) : _*).invoke(x, args.map(_.asInstanceOf[AnyRef]) : _*))
| }
| override def typed[T] = x.asInstanceOf[T]
| override def toString = "Dynamic(" + x.toString + ")"
| }
defined class DynamicImpl
scala>
scala> implicit def toDynamic(x: Any): Dynamic = new DynamicImpl(x.asInstanceOf[AnyRef])
toDynamic: (x: Any)Dynamic
scala> class Duck {
| def quack = "Quack!"
| }
defined class Duck
scala> class QuackingSwan {
| def quack = "Swack!"
| }
defined class QuackingSwan
scala> def makeQuack(d: Dynamic) {
| println(d.quack.typed[String])
| }
dynatype: d._select_("quack")
makeQuack: (d: Dynamic)Unit
scala> makeQuack(new Duck)
Quack!
scala> makeQuack(new QuackingSwan)
Swack!
scala> val s: Dynamic = "Hello, world!"
s: Dynamic = Dynamic(Hello, world!)
scala> s.toLowerCase
dynatype: line8$object.$iw.$iw.s._select_("toLowerCase")
res2: Dynamic = Dynamic(hello, world!)
scala> s.length
dynatype: line8$object.$iw.$iw.s._select_("length")
res3: Dynamic = Dynamic(13)
scala> s.isEmpty.typed[Boolean]
dynatype: line8$object.$iw.$iw.s._select_("isEmpty")
res4: Boolean = false
此处_select_
和_invoke_
是什么?
答案 0 :(得分:1)
_select_
和_invoke_
的行为类似于Ruby missing_method
。在未实现该方法的动态实例上调用任何方法时,将调用_select_
或_invoke_
。这两种方法可以按照您的意愿实现。
在您的示例中,他们使用反射来调用x
成员的实际实现。例如:
scala> val s: Dynamic = "Hello, world!"
s: Dynamic = Dynamic(Hello, world!)
scala> s.toLowerCase
dynatype: line8$object.$iw.$iw.s._select_("toLowerCase")
res2: Dynamic = Dynamic(hello, world!)
toLowerCase
中未定义方法s
。因此,使用参数_select_
调用方法"toLowerCase"
。然后,动态机制将使用反射调用x.toLowerCase
并将结果包装在新的动态实例中。
_select_
,而对任何带参数的方法调用_invoke_
。