Tuesday, December 17, 2013

ReactiveMongo Recipe: Awesome macros with Scala 2.10

Serialization is always a pain. There is always the trade-off between high performance, but manual static mapping code and more flexible libraries based on reflection. I usually go with the second approach because I think the potential of mistakes creeping in to manual mapping implementations far outweight the performance hit in many cases.

Scala macros now available in Scala 2.10 seem to provide us the best of both worlds by automatically generating the static mapping code at compile time. I got my first taste of this today using the 0.10.0-SNAPSHOT version of ReactiveMongo. To use them you can simply create a package.scala class that defines your implicit conversions from BSONDocument to your case classes and use Reactive's macros to generate this code: This should be highly performant and easy to manage. Just make sure you have any nested classes define their converters above their parent. Then in your DAO you can convert to your case class with no special code: This is super cool. I suspect there will be lots of really interesting uses for macros once Scala 2.10 gets more common.

No comments:

Post a Comment