有这个宏委托宏可能正是您正在寻找的。它的目标是自动实现委托/代理模式,因此在您的示例中,您的类B必须扩展 class A。
它是针对2.11、2.12和进行交叉编译的2.13。因为你必须使用宏天堂编译插件才能使其工作2.11。2.12对于2.13,您需要使用 flag-Ymacro-annotations代替。
像这样使用它:
trait Connection {
def method1(a: String): String
def method2(a: String): String
// 96 other abstract methods
def method100(a: String): String
}
@Delegate
class MyConnection(delegatee: Connection) extends Connection {
def method10(a: String): String = "Only method I want to implement manually"
}
// The source code above would be equivalent, after the macro expansion, to the code below
class MyConnection(delegatee: Connection) extends Connection {
def method1(a: String): String = delegatee.method1(a)
def method2(a: String): String = delegatee.method2(a)
def method10(a: String): String = "Only method I need to implement manually"
// 96 other methods that are proxied to the dependency delegatee
def method100(a: String): String = delegatee.method100(a)
}
它应该适用于大多数场景,包括涉及类型参数和多个参数列表的情况。
免责声明:我是宏的创建者。