Es scheint unmöglich zu sein, abstrakte Methoden über def Makros zu implementieren:Implementieren abstrakte Methoden mit einem def Makro
import scala.reflect.macros.Context
import language.experimental.macros
trait A {
def foo(): Unit
}
object AImpl {
def fooImpl(c: Context)(): c.Expr[Unit] = {
import c.universe._
c.Expr[Unit](reify().tree)
}
}
trait AImpl extends A {
def foo(): Unit = macro AImpl.fooImpl
}
Dies mit dem folgenden Fehler fehl:
[error] .../A.scala:17: overriding method foo in trait A of type()Unit;
[error] macro method foo cannot override an abstract method
[error] def foo(): Unit = macro AImpl.fooImpl
[error] ^
Wenn ich die extends A
es entfernen kompiliert. Aber offensichtlich möchte ich AImpl
erfüllen Merkmal A
. Wie behebe ich das?
Ein weiterer Versuch:
trait AImpl extends A {
def foo(): Unit = bar()
def bar(): Unit = macro AImpl.fooImpl
}
Gibt neue Fehler:
[error] macro implementation not found: bar (the most common reason for that is that
you cannot use macro implementations in the same compilation run that defines them)
[error] one error found
Benötigen Sie tatsächlich einen Aufruf an '(a: AImpl) .foo()', um ein Makro zu sein und auf der Website zu erweitern, oder müssen Sie nur den Inhalt von generieren die (normale) 'foo()' Methode? – gourlaysama