I am trying to annotate the constructor values of a class using macro-annotations. Assume that a macro-annotation called @identity is implemented and is used as follows in the class definition of class A:
class A(@identity val foo: String, // causes error
val bar: String) {
@identity val foobar: String = "" // doesn't cause error
}
When just annotating foobar everything compiles just fine. However, when annotating foo I get the following compile-time error:
top-level class without companion can only expand either into an eponymous class or into a block consisting in eponymous companions
Could someone elaborate on this error and why it occurs?
I suspect you call a macro
like
Then you have error
The thing is that you take a class (and possibly companion object) and return not only them but also
val fooso you change number/flavor of top-level definitions which is forbidden https://docs.scala-lang.org/overviews/macros/annotations.htmlFor example if we change the macro
then everything will compile.