Will it be possible to generate several top-level classes with one macro invocation in scala 2.10?

StackOverflow https://stackoverflow.com/questions/12295971

  •  30-06-2021
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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, objects) 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 objects are a superset of packages and type macros can generate objects that wouldn't be necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top