2
我想要做這樣的事情:實現超微距和擴大其在子類中
trait Endo {
def apply[T](x: T): T
}
trait SuperType {
def endoMap(f: Endo): SuperType = macro SuperTypeMacro.endoMapImpl
}
case class Foo(x: Int) extends SuperType {
// endoMapImpl expands to
// Foo(f(x))
}
case class Bar(x: Int, y: Boolean) extends SuperType {
// endoMapImpl expands to
// Bar(f(x), f(y))
}
理想情況下,我需要寫的唯一的事情就是extends SuperType
。可能嗎?如果不是,我相信宏觀註釋應該允許這樣做;我對嗎?
其實,我不確定這是否足夠。我只有'c.prefix'的接收器的靜態類型,對吧?如果我可以列舉'SuperType'的所有子類型,那很好,但是如果其他人擴展了'SuperType'而不重新編譯它,我就會遇到問題。 –
是的,只有靜態類型。你也可以存儲幫助派生類中的宏分支的信息 - 註釋,方法簽名等等。如果你能提供一個完整的用例,我可能會告訴更多。 –