14

I would like to create a macro generated hierarchy of sealed abstract and case classes. There was an example similar to this with http://docs.scala-lang.org/overviews/macros/typemacros.html but is is now obsolete. Is this still possible?

I think it would be incredibly powerful to generate a type safe AST for some specified grammar. Ideally with an IDE able to resolve all the classes.

1 Answer 1

25

First for some shameless self-promotion: Eugene Burmako and I are giving a talk on type providers, a closely related topic, at Scalar 2014 tomorrow, and I encourage you to take a look at the example project we put together for the talk if you're interested in this kind of thing.

While type macros are no longer supported, you can accomplish essentially the same thing with macro annotations from macro paradise (which is available as a plugin for Scala 2.10 and 2.11):

import scala.annotation.StaticAnnotation
import scala.language.experimental.macros
import scala.reflect.macros.Context

// Add constructor arguments here.
class expand extends StaticAnnotation {
  def macroTransform(annottees: Any*) = macro Expander.expand_impl
}

object Expander {
  def expand_impl(c: Context)(annottees: c.Expr[Any]*) = {
    import c.universe._

    annottees.map(_.tree) match {
      case List(q"trait $name") => c.Expr[Any](
        // Add your own logic here, possibly using arguments on the annotation.
        q"""
          sealed trait $name
          case class Foo(i: Int) extends $name
          case class Bar(s: String) extends $name
          case object Baz extends $name
        """
      )
      // Add validation and error handling here.
    }
  }
}

And then:

scala> @expand trait MyADT
defined trait MyADT
defined class Foo
defined class Bar
defined module Baz

You can add arguments to the annotation that will be available at compile time, allowing you to parse an external resource that you can use to generate the implementation of the ADT, for example.

Macro annotations are very experimental and their status is still up in the air—there's no guarantee that they'll ship with Scala 2.12, for example. Something similar (although not quite so clean) is possible using plain old def macros and structural types—see the example project linked above for more detail and some demonstrations. In any case, this kind of mechanism is of interest to many people, including the developers of Scala's macro system, so even if macro annotations disappear at some point down the road, there's likely to be some way to accomplish what you've described here.

6
  • 1
    Completely off topic: damn, I wish I knew earlier about that conference. I signed up, even though I doubt there will be any tickets left one day before the event :) Commented Apr 4, 2014 at 9:13
  • If there is a video of that presentation, it would be great to link it back here.
    – user833970
    Commented Apr 4, 2014 at 15:54
  • 2
    Thanks! With def macros, am I right in thinking that there would be no way to directly reference the created class in source?
    – user833970
    Commented Apr 4, 2014 at 17:20
  • 1
    This does not seem to work with Scala 2.12.1, even once the warnings are fixed. @expand trait Whatever or @expand case class Whatever will create a type, but unrelated to the code in the quasi quoted string. Commented Feb 14, 2017 at 20:31
  • 1
    For 2.11.7 I found I needed to include the Paradise jar in the REPL to get this to work e.g. wget http://central.maven.org/maven2/org/scalamacros/paradise_2.11.6/2.1.0/paradise_2.11.6-2.1.0.jar and scala -Xplugin:paradise_2.11.6-2.1.0.jar Commented Mar 3, 2017 at 18:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.