Metaprogramming with traits

  • Authors:
  • John Reppy;Aaron Turon

  • Affiliations:
  • University of Chicago;University of Chicago

  • Venue:
  • ECOOP'07 Proceedings of the 21st European conference on Object-Oriented Programming
  • Year:
  • 2007

Quantified Score

Hi-index 0.00

Visualization

Abstract

In many domains, classes have highly regular internal structure. For example, so-called business objects often contain boilerplate code for mapping database fields to class members. The boilerplate code must be repeated per-field for every class, because existing mechanisms for constructing classes do not provide a way to capture and reuse such member-level structure. As a result, programmers often resort to ad hoc code generation. This paper presents a lightweight mechanism for specifying and reusing member-level structure in Java programs. The proposal is based on a modest extension to traits that we have termed trait-based metaprogramming. Although the semantics of the mechanism are straightforward, its type theory is difficult to reconcile with nominal subtyping. We achieve reconciliation by introducing a hybrid structural/nominal type system that extends Java's type system. The paper includes a formal calculus defined by translation to Featherweight Generic Java.