Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 吸血鬼方法、类型提供程序宏和缺少runtime.VolatileObjectRef_Scala_Scala Macros_Scala Macro Paradise - Fatal编程技术网

Scala 吸血鬼方法、类型提供程序宏和缺少runtime.VolatileObjectRef

Scala 吸血鬼方法、类型提供程序宏和缺少runtime.VolatileObjectRef,scala,scala-macros,scala-macro-paradise,Scala,Scala Macros,Scala Macro Paradise,我正在尝试编写一个类型提供程序宏,它使用所描述的吸血鬼方法技巧;这是我的实现的一个最小模型: object DeriveFamily { def minimal: Any = macro DeriveFamilyMacros.minimal } object DeriveFamilyMacros { class body(tree: Any) extends StaticAnnotation def bodyImpl(c: Context) = { import c.un

我正在尝试编写一个类型提供程序宏,它使用所描述的吸血鬼方法技巧;这是我的实现的一个最小模型:

object DeriveFamily {
  def minimal: Any = macro DeriveFamilyMacros.minimal
}

object DeriveFamilyMacros {
  class body(tree: Any) extends StaticAnnotation

  def bodyImpl(c: Context) = {
    import c.universe._

    val field = c.macroApplication.symbol
    val bodyAnn = field.annotations.filter(_.tree.tpe <:< typeOf[body]).head
    bodyAnn.tree.children.tail.head
  }

  def minimal(c: Context): c.Tree = {
    import c.universe._
    q"""object Foobar { val _x = "X"; @DeriveFamilyMacros.body(Foobar._x) def x: String = macro DeriveFamilyMacros.bodyImpl }; Foobar"""
  }
}

非常欢迎您提供任何帮助,谢谢。

我对宏不太熟悉,但我记得看到过类似的东西。在
minimal
中使用类而不是对象不是解决了这个问题吗?
object Working {
  def x = {
    val foobar = DeriveFamily.minimal
    println(foobar.x)
  }

  x // Prints "X"
}

object NotWorking {
  val foobar = DeriveFamily.minimal
  println(foobar.x)
  /*
  [trace] Stack trace suppressed: run last common/test:compileIncremental for the full output.
  [error] (common/test:compileIncremental) java.lang.IllegalArgumentException: Could not find proxy for var Foobar$module: runtime.VolatileObjectRef in List(variable Foobar$module, value foobar, Object NotWorking, package <root>) (currentOwner= value <local NotWorking> )
  */
}