Unification of Compile-Time and Runtime Metaprogramming in Scala

Metaprogramming is a technique that consists in writing programs that treat other programs as data. This paradigm of software development contributes to a multitude of approaches that improve programmer productivity, including code generation, program analysis and domain-specific languages. Many programming languages and runtime systems provide support for metaprogramming. Programming platforms often distinguish the notions of compile-time and runtime metaprogramming, depending on the phase of the program lifecycle when metaprograms execute. It is common for different lifecycle phases to be hosted in different environ- ments, so it is also common for different kinds of metaprogramming to provide different capabilities to metaprogrammers. In this dissertation, we present an exploration of the idea of unifying compile-time and runtime metaprogramming in Scala. We focus on the practical aspect of the exploration; most of the described designs are available as popular software products, and some of them have become part of the standard distribution of Scala. First, guided by the motivation to consolidate disparate metaprogramming techniques available in earlier versions of Scala, we introduce scala.reflect, a unified metaprogram- ming framework that uses a language model derived from the Scala compiler to run metaprograms both at compile time and at runtime. Secondly, armed by the newfound metaprogramming powers, we describe Scala macros, a language-integrated compile-time metaprogramming facility based on scala.reflect. Thanks to the comprehensive nature of scala.reflect, macros are able to work with both syntactic and semantic information about Scala programs, enabling a wide range of previously impractical or impossible use cases. Finally, based on our experience and user feedback, we identify key strengths and weaknesses of scala.reflect and macros. We propose scala.meta, a new unified metapro- gramming framework, and inline/meta, a new macro system based on scala.meta, that take the best from their predecessors and address the most important problems.

    Keywords: Programming Languages ; Compilers ; Metaprogramming ; Reflection ; Macros

    Thèse École polytechnique fédérale de Lausanne EPFL, n° 7159 (2017)
    Programme doctoral Informatique et Communications
    Faculté informatique et communications
    Laboratoire de méthodes de programmation 1
    Jury: Prof. James Richard Larus (président) ; Prof. Martin Odersky (directeur de thèse) ; Prof. Viktor Kuncak, Dr Don Syme, Prof. Sam Tobin-Hochstadt (rapporteurs)

    Public defense: 2017-3-6


    Record created on 2017-03-01, modified on 2017-05-12

Related material