Data types "a la carte"

upd. Macro paradise for 2.10 and 2.11 now features macro annotations that can implement the design that's discussed below.

Besides being useful for type providers and large-scale code generation, macro types provide another interesting opportunity for both a language designer and a programmer.

Recall that macro types can be mixed in into abritrary classes, and let's think this idea over. We've already seen how mixing in independently generated logic can be useful for real-world programming tasks, but what if macro types would generate the code and be aware of their context?

An augmentation of macro types lets them read the contents (i.e. the members) of the host class. This is immediately useful for building so called data types "a la carte". Take case classes. Write the following code and inspect the output:

echo 'case class Foo(bar: String, baz: Int)' >> Hello.scala
scalac -Xprint:typer Hello.scala

We see that the compiler has generated a lot of stuff for a small case class declaration, i.e. among the others implementations of Product, Serializable, equals/hashCode, toString, as well as, the entire companion object. Certain auto-implementations are frequently useful on their own, but, currently, it's all or nothing - either you declare your class as a case class and live with all restrictions being imposed, or you don't get any of the auto-generated goodies.

A more balanced API can be provided by augmented macro types or, alternatively, macro annotations. Such macros can discover members of the main class, so they can provide auto-generated functionality in piecemeal fashion. This restaurant metaphor gives the technique its fancy name.