我在回答this question。如何將一個無參數構造函數添加到具有宏註解的Scala案例類中?
而是寫的:
case class Person(name: String, age: Int) {
def this() = this("",1)
}
我想我會用宏註釋從展開:
@Annotation
case class Person(name: String, age: Int)
所以我嘗試添加新的構造函數作爲一個純老DefDef
使用quasiquotes在宏註釋的impl中,如:
val newCtor = q"""def this() = this("", 1)"""
val newBody = body :+ newCtor
q"$mods class $name[..$tparams](..$first)(...$rest) extends ..$parents { $self => ..$newBody }"
但是那個返回ns錯誤:called constructor's definition must precede calling constructor's definition
有沒有辦法解決這個問題?我錯過了什麼?
感謝您抽空看看, -Julian
唉,我使用天堂2.0.0-M6爲2.10.3。 [Here](https://github.com/julianpeeters/macro-annotation-example/tree/no-args)是一個最小的可運行示例。並且[這裏](https://gist.github.com/julianpeeters/9888898)是M4中的錯誤。 –
哦,我明白了。所以我們在這裏有兩個錯誤。一個由M6固定,另一個依然存在。 –
https://github.com/julianpeeters/macro-annotation-example/pull/1 –