Macro class name expansion in Scala 3
Asked Answered
B

1

0

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.

Birdsall answered 26/9, 2022 at 1:53 Comment(0)
E
1

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.

Eau answered 26/9, 2022 at 2:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.