Home > Blockchain >  Macro class name expansion in Scala 3
Macro class name expansion in Scala 3

Time:09-27

I am trying to use the macros with quoted DSL for generating a class like this —

val name = ???

'{
  final class ${name} {
    def foo: String = ...
    def bar: Int = ...
  }
}

The idea is to generate a class with a particular name. If name is of type String then the scala compiler throws syntax error. Same is true for Expr.

What should be the type of name? Expr doesn't work.

CodePudding user response:

With a quote you can create only a class with statically known name (the code inside a quote must typecheck). If the name of a class is a string you should go deeper and use reflection API

Method Override with Scala 3 Macros

Please notice that the class will be generated inside a block (Scala 3 macros are def macros), so will be visible only there.

  • Related