トレイトの実装を生成するメソッドを作成したいと思います。例えば:
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クラスを生成できるようにしたいと思います。クラスに注釈を付けたり、コンパイラプラグインを作成したりすることを考えていましたが、もっと簡単な方法があるのではないでしょうか。任意のポインタをいただければ幸いです。