So you write 4 lines for this case class.You know what?We have had a few complaints from some people who think it’s not cool to write a Reads[TheirClass] because usually Java JSON frameworks like Jackson or Gson do it behind the curtain without writing anything.We argued that Play2.1 JSON serializers/deserializers are:

completely typesafe,

fully compiled,

nothing was performed using introspection/reflection at runtime.

But for some, this didn’t justify the extra lines of code for case classes.

We believe this is a really good approach so we persisted and proposed:

Case Class Inspection

As you may deduce by yourself, in order to ensure preceding code equivalence, we need :

to inspect Person case class,

to extract the 3 fields name, age, lovesChocolate and their types,

to resolve typeclasses implicits,

to find Person.apply.

INJECTION?

No I stop you immediately…

Code injection is not dependency injection…No Spring behind inception… No IOC, No DI… No No No ;)

I used this term on purpose because I know that injection is now linked immediately to IOC and Spring. But I’d like to re-establish this word with its real meaning.Here code injection just means that we inject code at compile-time into the compiled scala AST (Abstract Syntax Tree).

This is enabled by a new experimental feature introduced in Scala 2.10: Scala Macros

Scala macros is a new feature (still experimental) with a huge potential. You can :

introspect code at compile-time based on Scala reflection API,

access all imports, implicits in the current compile context

create new code expressions, generate compiling errors and inject them into compile chain.

Please note that:

We use Scala Macros because it corresponds exactly to our requirements.

We use Scala macros as an enabler, not as an end in itself.

The macro is a helper that generates the code you could write by yourself.

It doesn’t add, hide unexpected code behind the curtain.

We follow the no-surprise principle

As you may discover, writing a macro is not a trivial process since your macro code executes in the compiler runtime (or universe).

So you write macro code
that is compiled and executed
in a runtime that manipulates your code
to be compiled and executed
in a future runtime…

That’s also certainly why I called it Inception ;)

So it requires some mental exercises to follow exactly what you do. The API is also quite complex and not fully documented yet. Therefore, you must persevere when you begin using macros.

I’ll certainly write other articles about Scala macros because there are lots of things to say.This article is also meant to begin the reflection about the right way to use Scala Macros.Great power means greater responsability so it’s better to discuss all together and establish a few good manners…