我正在尝试回答这个问题 https://stackoverflow.com/questions/13812172/how-can-i-create-an-instance-of-a-case-class-with-constructor-arguments-with-no.
而不是写:
case class Person(name: String, age: Int) {
def this() = this("",1)
}
我想我应该使用宏注释来扩展它:
@Annotation
case class Person(name: String, age: Int)
所以我尝试将新的构造函数添加为普通的旧构造函数DefDef
在宏注释的 impl 中使用准引号,例如:
val newCtor = q"""def this() = this("", 1)"""
val newBody = body :+ newCtor
q"$mods class $name[..$tparams](..$first)(...$rest) extends ..$parents { $self => ..$newBody }"
但这会返回一个错误:called constructor's definition must precede calling constructor's definition
有办法解决这个问题吗?我错过了什么?
谢谢参观,
——朱利安
事实证明,在宏注释中生成辅助构造函数的非常自然的意图暴露了两个不同的问题。
1)第一期(https://issues.scala-lang.org/browse/SI-8451 https://issues.scala-lang.org/browse/SI-8451) 是关于准引号为辅助构造函数发出错误的树形状。该问题已在 2.11.0-RC4(尚未发布,目前以 2.11.0-SNAPSHOT 形式提供)和 2.10.x 的天堂 2.0.0-M6(昨天发布)中得到修复。
2)第二个问题是关于未分配的位置在类型检查期间造成严重破坏。奇怪的是,当对构造函数的调用进行类型检查时,打字机使用位置来确定这些调用是否合法。这不是那么容易修补的,我们必须解决以下问题:
val newCtor = q"""def this() = this(List(Some("")))"""
- val newBody = body :+ newCtor
+
+ // It looks like typer sometimes uses positions to decide whether stuff
+ // (secondary constructors in this case) typechecks or not (?!!):
+ // https://github.com/xeno-by/scala/blob/c74e1325ff1514b1042c959b0b268b3c6bf8d349/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L2932
+ //
+ // In general, positions are important in getting error messages and debug
+ // information right, but maintaining positions is too hard, so macro writers typically don't care.
+ //
+ // This has never been a problem up until now, but here we're forced to work around
+ // by manually setting an artificial position for the secondary constructor to be greater
+ // than the position that the default constructor is going to get after macro expansion.
+ //
+ // We have a few ideas how to fix positions in a principled way in Palladium,
+ // but we'll have to see how it goes.
+ val defaultCtorPos = c.enclosingPosition
+ val newCtorPos = defaultCtorPos.withEnd(defaultCtorPos.endOrPoint + 1).withStart(defaultCtorPos.startOrPoint + 1).withPoint(defaultCtorPos. point + 1)
+ val newBody = body :+ atPos(newCtorPos)(newCtor)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)