I have an program with lots of boilerplate (which is, sadly, not reducible even by Scala mechanisms). But if there would be a way to generate complex top-level classes with a macro, all that boilerplate will go away. For example:
package org.smth
generate(params)
// becomes
class A { ... }
object B { ... }
case class C { ... }
Will it be possible with Scala 2.10 macros?
In short: no.
Macro types (i.e. macros that generate types instead of methods) are planned, but they are not designed nor specified, let alone implemented yet, and they won't be for 2.10.
Also, a single macro invocation can only generate a single type. However, since types (specifically, object
s) can be nested, this is not a limitation: you can just generate a single 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 that can generate entire packages full of classes, but realized that since object
s are a superset of package
s and type macros can generate object
s that wouldn't be necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With