Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 807
Rogach Avatar asked Sep 06 '12 08:09

Rogach


1 Answers

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.

like image 50
Jörg W Mittag Avatar answered Nov 15 '22 21:11

Jörg W Mittag