Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.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 宏:在编译时检查类属性_Scala_Scala Macros - Fatal编程技术网

Scala 宏:在编译时检查类属性

Scala 宏:在编译时检查类属性,scala,scala-macros,Scala,Scala Macros,我正在使用宏,我想检查编译时间,如果创建的实例类包含构造函数中传递的所有属性,或者其中一个为null,如果是这种情况,则在编译时间内检查,而不是在运行时检查 我已经阅读了很多关于它的博客,但我没有发现任何关于这个特定场景的内容,所以在编译时可能是不可行的 如果可能的话,如果有人知道一个博客,请阅读有关如何使用AST的文章,我将不胜感激 这是我的建筑课 actorBuilder = F2eActorBuilder .withApplicationConf(new File(applicatio

我正在使用宏,我想检查编译时间,如果创建的实例类包含构造函数中传递的所有属性,或者其中一个为null,如果是这种情况,则在编译时间内检查,而不是在运行时检查

我已经阅读了很多关于它的博客,但我没有发现任何关于这个特定场景的内容,所以在编译时可能是不可行的

如果可能的话,如果有人知道一个博客,请阅读有关如何使用AST的文章,我将不胜感激

这是我的建筑课

actorBuilder = F2eActorBuilder
  .withApplicationConf(new File(applicationContext))
  .withAkkaContext( "akkaContext.xml")
  .withApplicationContext("applicationContext.xml")
  .build()
一旦定义了actorBuilder,我就要检查它,因为它是一个DSL,并且一些带有*的可能没有被调用,所以如果其中一个属性被设置为null,我想在编译时检查它

这是我的宏代码

  import scala.language.experimental.macros

  def valid(action: F2eActorBuilder): F2eActorBuilder = macro checkActionImpl

  def checkActionImpl(c: blackbox.Context)(action: c.Tree): c.Tree = {
    import c.universe._
    def isValidAction(s: F2eActorBuilder): Boolean = checkMessage(s)

    action match {
      case _tree@Literal(Constant(s: F2eActorBuilder)) if isValidAction(s) => _tree
      case _tree@Literal(Constant(s: F2eActorBuilder)) => c.abort(c.enclosingPosition, getErrorMessage(s))
    }
  }

  def checkMessage(action: F2eActorBuilder): Boolean = {
    action.akkaContextPath != null
  }

  def getErrorMessage(action: F2eActorBuilder): String = {
    s"Some mandatory attributes are marked as null"
  }

请给出更详细的示例,说明您正在尝试做什么。您好,我已经做了,看看它是否对您有意义,我还会粘贴我的宏代码,一般情况下,您所要求的是不可能的,因为您只能防止显式传递的null literal-这可以通过使用像除疣器这样的线头更容易实现。但是,如果您从某处传递参数:作为参数或某个计算的结果,如果不验证代码可以采用的所有路径,您将无法验证它。基本上,只要语言允许null,就不能在编译时完全消除它。