GSoC 2017


Project maintained by vlthr Hosted on GitHub Pages — Theme by mattgraham

A Summer of Scala

Over the past few months I’ve had the opportunity to work with the Scala organization as part of the Google Summer of Code program. It all started when Felix Mulder held a guest lecture at my university’s main compiler course, showing off all the cool things the Scala team were doing with Dotty. I was immediately interested, but as so often happens with interesting things it ended up being pushed to the side with all the other interesting things that you never quite have time to learn more about. It wasn’t until about a year later when I heard that the Scala organization was looking for applicants to their GSoC program that I finally got in touch with Felix and asked how I could contribute. After picking an open issue on Dotty’s tracker and submitting a couple of pull requests, we discussed a bunch of ideas for possible GSoC projects. I picked one, put together a proposal, submitted it, and it got accepted!

When summer finally came, some of the groundwork required for my proposal to be implemented was still unfinished, and rather than taking the risk of needing to rewrite the project by the end of the summer, we decided to tackle a different problem instead: Writing a template rendering engine using Dotty. The aim of the project was twofold. First, to test the compiler on a medium sized project by finding and reproducing error cases. Second, to use the templating engine to provide stricter compile-time safety for DottyDoc, Dotty’s built in documentation generator.

Why Dotty?

Dotty is the experimental new compiler the Scala team is working on to one day replace scalac as the main Scala compiler. The reasons for doing a complete rewrite are varied, but in short, Dotty’s type system is built on the newly developed DOT calculus, giving it firmer theoretical underpinnings. The compiler now also splits its work into more distinct mini-phases which can be parallellized to a greater extent than before, allowing for faster compile times. These may not be the first thing an end user thinks about when using the compiler but they give rise to a host of implications for reliability, performance and ease of development.

Apart from these big picture issues, Dotty also brings a range of new features that are more visible to the end user. The complete list can be seen on the Dotty homepage but here are some of my favorites:

Show and tell

The end result is Levee, a standalone templating engine for the Liquid templating language. The library is now available on Sonatype. In addition to providing strict rendering of Liquid templates, Levee allows users to define type safe filters without requiring the user to handle type checking.

// For the liquid filter Hello World
// Filter takes three type arguments for the Input, Args, and Optional Args respectively
// HNil is the type equivalent of a Nil, indicating the end of a list of types
val prepend = Filter[StringValue, StringValue :: HNil, HNil]("prepend") {
  (ctx, filter, input, args, optArgs) =>
    val prefix = args.head.get
    success(StringValue(prefix + input.get))
}

It was this custom filter feature that ended up taking most of my time, since for a long time every solution I could come up with required the user to either manually handle the type checking, or do some sort of unsafe casting of the inputs to the filter. After weeks of trying I was introduced to Shapeless, a library that provides a set of tools for doing generic programming in Scala. It took a few days to wrap my head around how the library works, but once it clicked I was finally able to solve the problem in a way I was happy with.

In addition to the code in the repository, the following issues on the Dotty repository describe and reproduce crashes that were discovered during the course of the project:

The library is usable, but not quite feature complete. The remaining work is summarized in the repository issue tracker, with the following being most important:

Lessons learned

Looking back, there are a few things that I wish I had given more thought to early on in the project.

Overall, my experience with Dotty was that for an experimental compiler it is surprisingly stable. Over the course of the summer I encountered a total of two compiler crashes and one OOM error, but out of these, two were related to some very unusual and experimental implicit resolution code, and only one of the crashes was caused by sound Scala code. While it may be too early to move your production codebase to Dotty, it may not be long until the expanded set of features makes the jump worthwhile.