简体   繁体   中英

Macro class name expansion in Scala 3

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM