Macro-supported DSLs for schema bindings

We’ve recently started using the W3C‘s banana-rdf library at MITH, and it’s allowing us to make a lot of our code for working with RDF graphs both simpler and less tightly coupled to a specific RDF store. It’s a young library, but also very clever and well-designed, and it does an excellent job of exploiting advanced features of the Scala language to make its users’ lives easier. Alexandre Bertails and his collaborators deserve a lot of credit for what they’ve accomplished in just a little over a year.

One of the least pleasant aspects of working with any RDF library is writing bindings for particular vocabularies. For example, if we wanted to use the Open Archives Initiative’s Object Reuse and Exchange vocabulary in our banana-rdf application, we’d need to write something like the following:

This isn’t as bad as writing a Jena vocabulary, for example, but it’s still verbose and error-prone. A relatively simple application can require dozens of these vocabulary bindings, some of which may define hundreds of classes and properties. The vocabularies may also be under development and subject to change, in which case the binding code must be kept in sync with a schema or some other kind of documentation.

Since this is Scala, I decided that I’d take a stab at using macros to make the process of writing these things a little easier. It’s not too hard to use this trick to write a macro that will parse a given RDF schema at compile time and generate an anonymous subclass of Prefix with the appropriate members. The usage looks like this:

And then:

No need to type out all the class and property names twice—we just drop the schema in our resource folder, point to it when we call our macro method, and we’re ready to go. Note that we also don’t have to provide the prefix name—it will be set automatically based on the name of the value ("ore" in this case).

This is simple and easy, but it’s not perfect. We don’t have a “real” type for our ORE Prefix—just the structural type of the ore instance. This can be inconvenient in certain situations, and it also means that writing something like ore.similarTo involves a reflective call. It’s still entirely type-safe—we’d get a compile-time error if we accidentally wrote ore.similarToo, for example—but it does inflict a small performance penalty, and if we don’t want lots of warnings during compilation we either have to set a compiler option to enable reflective calls or import scala.language.reflectiveCalls all over the place.

Someday we’ll all live in Macro Paradise and type macros will make this kind of problem completely irrelevant. In the meantime, I’ve been experimenting with a couple of other approaches that avoid the problem while still minimizing verbosity. We have to write out all of our class and property names, but only once:

Next we call a macro method that will create an instance of this trait with the appropriate implementations of these values:

And now we can run the REPL code above without any reflective calls.

If we want a little more compile-time protection against typing mistakes, we can point to a schema and the macro will confirm that all of our class and property names are legitimate:

Suppose for example that this had been our trait:

We’d get a nice little error when we try to compile MyPrefixes:

This is currently all experimental work, and I’m not sure whether any of it will make its way into projects at MITH, but I’ve published the code on GitHub in case anyone’s interested in taking a look at how it’s implemented.