A series on Phantom - Part 5: What's new in Phantom 2.0.0
Flavian
Flavian Scala Developer
Flavian is a Scala engineer with many years of experience and the author of phantom and morpheus.

If you are completely new to phantom or a non-techie, the best place to start learning about phantom is probably the official page here. This article will be short but in depth overview of what the 2.0 roadmap looks like and what cool new toys are now available!

1. Goodbye reflection, hello implicit macros

One of the most important improvements in the long anticipated 2.0.0 release is the complete replacement of reflection with implicit macros. At first this may not sound very interesting, but it's certainly one of the more complex and beneficial improvements we were able to offer.

It means the dreaded scala-reflect dependency is now a thing of the past and at the same time the global Lock forced greedy initialisation of objects that was necessary until now for phantom to "learn" about the columns of your custom schema.

Instead of relying on reflection to analyse the inner content of a Cassandra table or the contents of a Database object and introduce 2 layers of global locking to work around the thread safety of reflection, we have the opportunity to easily explore compile time meta-programming to generate the necessary list of columns in a table or tables in a database at compile time, on the fly, and in a manner invisible to you, the end user.

2. Primitive generation is now automated

To convert from your custom data models to something Cassandra understands, or more Java-esque typing to talk to the Java Driver, phantom uses a typeclass and an ADT based on primitives. They encapsulate things like serialization information, Java -> Scala conversions, and more.

And they are found throughout phantom, but until now we had no way to automatically derive primitives for known types. This is because we were relying on helper methods such as enumPrimitive and so on. This has several consequences, one of the more interesting ones being that you can very easily derive new types based on existing ones.

case class Status(name: String)

implicit val statusPrimitive = {
Primitive.derive[Status, String](_.name)(Status.apply)
}

3. The "store" method is now auto-generated

One more of the notoriously boilerplatey things about phantom is that you have to had to define a manual insert method even though the record type was specified already as a type argument. This is kind of pointless, and the reason for that is because without macros it would have been very resource intensive to have to perform the mapping at runtime via reflection.

With macros in play however, this becomes a far more interesting and accessible problem. For more information on how this works, read the official docs here.

4. The "fromRow" method is now autogenerated

Another thing made possible by macros is the auto-generation of the "fromRow" method which is really cool, as it allows you to skip even more boilerplate from your code. We have put a lot of time and effort into making this method super efficient and work as you expected it to on the fly without any kind of manual interference.

It is still not perfect, feedback and bug reports are more than welcome via GitHub, but in the interim for 99% of all your uses cases you can enjoy an automated macro derived DSL that's as boilerplate free as it can be. We have far more ambitious plans for phantom 3.x series, but let's not get ahead of ourselves just yet.

5. Phantom Pro sees its very first live release

And to kick things off, we are finally ready to release a first cut of phantom-pro, showcasing full automatic UDT support via annotations, as well as support for automated database migrations in test mode. This will be an immense productivity boost for all of you Cassandra users out there, and fully type safe UDT support is a very interesting way to eliminate boilerplate.

UDTs are a feature you can use trivially, with the combination of case classes and UDTColumn. All it takes is to turn a case class into an UDT is the @Udt annotation, which makes it incredibly straightforward to generate any kind of complex patterns you want. This is a somewhat similar to Json.format in Play framework, and the underlying requirement is that every field in your case class is also of a known or derivable primitive type. We exploit implicit lookup to make complex typing possible.

Now at version 0.3.1, phantom-pro is seeing huge amounts of work and improvement, and we are working tirelessly to bring you an entire platform at your disposal, allowing you to subscribe to phantom-pro, manage your Cassandra cluster, your schema, have a rich set of resources, examples and manuals to allow you to be super productive, and much more!

We would like this opportunity to thank all our many initial adopters that are paying for our products, we look forward to your feedback and to a forever improving tool that you can leverage to help your engineering team succeed with Cassandra!

Want to learn more?

As official Datastax partners, Outworkers offers a comprehensive range of professional training services for Apache Cassandra and Datastax Enterprise, taking your engineering team from Cassandra newbies to full blown productivity in record time. Our example driven courses are the weapon of choice for companies of any size and if you happen to be a Scala user, we will also throw in a professional training session on using phantom in your company. All of our face-to-face training courses come with free ongoing access to our online training material.

For enquiries and bookings, please contact us by email at office@outworkers.com.

Related articles