我想創建一個生成特徵實現的方法。例如:從特徵中自動生成一個Scala類
trait Foo {
def a
def b(i:Int):String
}
object Processor {
def exec(instance: AnyRef, method: String, params: AnyRef*) = {
//whatever
}
}
class Bar {
def wrap[T] = {
// Here create a new instance of the implementing class, i.e. if T is Foo,
// generate a new FooImpl(this)
}
}
我想動態生成FooImpl
類,像這樣:
class FooImpl(val wrapped:AnyRef) extends Foo {
def a = Processor.exec(wrapped, "a")
def b(i:Int) = Processor.exec(wrapped, "b", i)
}
手動實現每個性狀的不是我們想(大量的樣板),所以我想喜歡能夠在編譯時生成Impl類。我正在考慮對類進行註釋,或者編寫一個編譯器插件,但也許有一種更簡單的方法?任何指針將不勝感激。
謝謝!這看起來不錯,我會試試看。 – 2012-08-05 18:19:52
工作很好!謝謝! – 2012-08-05 20:00:04