8

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?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Rogach
  • 26,050
  • 21
  • 93
  • 172

1 Answers1

7

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.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653