Is it possible to create multiple top-level classes with one macro call in scala 2.10?

I have a program with a large number of templates (which, unfortunately, cannot be reduced even to Scala mechanisms). But if there was a way to generate complex top-level classes with a macro, all of these patterns would disappear. For example:

package org.smth generate(params) // becomes class A { ... } object B { ... } case class C { ... } 

Will it be possible with Scala 2.10 macros?

+8
macros scala
source share
1 answer

In short: no.

Macro networks (i.e. macros that generate types instead of methods), but they are not designed and not defined, not to mention implemented ones, and they will not be for 2.10.

In addition, a single macro call can only generate one type. However, since types (in particular, object s) can be nested, this is not a limitation: you can simply create one top level object containing all the classes you need. The difference between this and your code is basically one additional import statement:

 package org.smth type O = Generate(params) // becomes object O { class A { ... } object B { ... } case class C { ... } } // which means you need an additional import O._ 

They thought about package macros, which can generate entire packages full of classes, but realized that since object is a superset of package , and type macros can generate object that are not needed.

+7
source share

All Articles