我试图用Scala反射工具箱编译一个带有DynamicMessage的Actor
演员代码如下
import scala.reflect.runtime.universe
import scala.tools.reflect.ToolBox
val toolbox = universe.runtimeMirror(getClass.getClassLoader).mkToolBox()
val actorCode =
"""
|import akka.actor._
|import com.google.protobuf._
|class SimpleActor extends Actor {
|override def receive: Receive = {
| case dynamicMessage: DynamicMessage => println("Dynamic message received!")
| case _ => println("Whatever!") // the default, catch-all
| }
|}
|object SimpleActor {
|def props() : Props = Props(new SimpleActor())
|}
|
|
|return SimpleActor.props()
|""".stripMargin
val tree = toolbox.parse(actorCode)
toolbox.compile(tree)().asInstanceOf[Props]
我得到了错误
reflective compilation has failed:
illegal cyclic reference involving type T
scala.tools.reflect.ToolBoxError: reflective compilation has failed:
illegal cyclic reference involving type T
如果我在工具箱之外运行代码,它会编译并正常工作。错误是从行中给出的
case dynamicMessage: DynamicMessage => println("Dynamic message received!")
有谁知道这个错误的性质以及如何修复它?
1条答案
按热度按时间ljsrvy3e1#
在Scala中,即使没有反射编译,也有结合Scala java互操作和F-有界多态性的bugs:
scalac reports error on valid Java class: illegal cyclic reference involving type T
以及其他。
com.google.protobuf.DynamicMessage
的父母探索F-bounded polymorphism:但是如果没有反射编译,代码将编译。因此,这是反射编译、scala java互操作和F-bounded多态性结合的缺陷。
解决方法是使用真实编译器而不是工具箱:
Tensorflow in Scala reflection(这里有一个类似的情况,在反射编译、Scala Java互操作和路径依赖类型的组合中出现了一个bug)
Dynamic compilation of multiple Scala classes at runtime
How to eval code that uses InterfaceStability annotation (that fails with "illegal cyclic reference involving class InterfaceStability")?(在反射编译期间也称为“非法循环引用”)
Scala Presentation Compiler - Minimal Example
(*)Protocol buffer objects generated at runtime