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.
Preview
Unable to display preview. Download preview PDF.
Similar content being viewed by others
References
Ancona, D., Damiani, F., Drossopoulou, S., Zucca, E.: Polymorphic bytecode: compositional compilation for Java-like languages. In: POPL’05, pp. 26–37 (2005)
Ancona, D., Lagorio, G., Zucca, E.: Jam–designing a Java extension with mixins. TOPLAS 25(5), 641–712 (2003)
Bono, V., Bugliesi, M., Liquori, L.: A Lambda Calculus of Incomplete Objects. In: MFCS, pp. 218–229 (1996)
Bracha, G.: The Programming Language Jigsaw: Mixins, Modularity and Multiple Inheritance. PhD thesis, University of Utah (March 1992)
Bracha, G., Cook, W.: Mixin-based inheritance. In: ECOOP’90, October 1990, pp. 303–311. ACM Press, New York (1990)
Czarnecki, K., Eisenecker, U.W.: Generative programming: methods, tools, and applications. ACM Press/Addison-Wesley Publishing Co, New York (2000)
Draheim, D., Lutteroth, C., Weber, G.: An Analytical Comparison of Generative Programming Technologies. Technical Report B-04-02, Institute of Computer Science, Freie Universität Berlin (January 2004)
Draheim, D., Lutteroth, C., Weber, G.: A Type System for Reflective Program Generators. In: Glück, R., Lowry, M. (eds.) GPCE 2005. LNCS, vol. 3676, pp. 327–341. Springer, Heidelberg (2005)
Ducasse, S., Nierstrasz, O., Schärli, N., Wuyts, R., Black, A.: Traits: A Mechanism for fine-grained Reuse. TOPLAS 28(2), 331–388 (2006)
Fähndrich, M., Carbin, M., Larus, J.R.: Reflective program generation with patterns. In: GPCE’06, pp. 275–284. ACM Press, New York (2006)
Fisher, K., Reppy, J.: Statically typed traits. Technical Report TR-2003-13, Dept. of Computer Science, U. of Chicago, Chicago, IL (December 2003)
Fisher, K., Reppy, J.: A typed calculus of traits. In: FOOL11 (January 2004)
Fisher, K., Reppy, J.H.: Extending Moby with Inheritance-Based Subtyping. In: Bertino, E. (ed.) ECOOP 2000. LNCS, vol. 1850, pp. 83–107. Springer, Heidelberg (2000)
Flatt, M., Findler, R.B., Felleisen, M.: Scheme with Classes, Mixins, and Traits. In: Kobayashi, N. (ed.) APLAS 2006. LNCS, vol. 4279, Springer, Heidelberg (2006)
Hofmann, M.: Syntax and Semantics of Dependent Types. In: Semantics and Logics of Computation, vol. 14, pp. 79–130. Cambridge University Press, Cambridge (1997)
Huang, S.S., Zook, D., Smaragdakis, Y.: Statically Safe Program Generation with SafeGen. In: Glück, R., Lowry, M. (eds.) GPCE 2005. LNCS, vol. 3676, pp. 309–326. Springer, Heidelberg (2005)
Igarashi, A., Pierce, B.C., Wadler, P.: Featherweight Java: a minimal core calculus for Java and GJ. TOPLAS 23(3), 396–450 (2001)
Kiczales, G., des Rivieres, J., Bobrow, D.G.: The art of metaobject protocol. MIT Press, Cambridge, MA (1991)
Kiczales, G., Lamping, J., Menhdhekar, A., Maeda, C., Lopes, C., Loingtier, J.-M., Irwin, J.: Aspect-Oriented Programming. In: Aksit, M., Matsuoka, S. (eds.) ECOOP 1997. LNCS, vol. 1241, pp. 220–242. Springer, Heidelberg (1997)
Liquori, L., Spiwack, A.: Feathertrait: A modest extension of featherweight java. TOPLAS (to appear, 2007)
Malayeri, D., Aldrich, J.: Combining Structural Subtyping and External Dispatch. In: FOOL/WOOD’07 (2007)
Nierstrasz, O., Ducasse, S., Schärli, N.: Flattening Traits. Journal of Object Technology 5(4), 129–148 (2006)
Odersky, M., Altherr, P., Cremet, V., Dragos, I., Dubochet, G., Emir, B., McDirmid, S., Micheloud, S., Mihaylov, N., Schinz, M., Stenman, E., Spoon, L., Zenger, M.: An overview of the Scala programming language. 2nd edn. Technical Report LAMP-REPORT-2006-001, EPFL, Lausanne, Switzerland (May 2006)
Quitslund, P.J.: Java traits — improving opportunities for reuse. Technical Report CSE 04-005, OGI School of Science & Engineering (September 2004)
Reppy, J., Turon, A.: A foundation for trait-based metaprogramming. In: FOOL/WOOD’06 (2006)
Riecke, J.G., Stone, C.A.: Privacy via subsumption. INC (A preliminary version appeared in FOOL5) 172(1), 2–28 (2002)
Smith, C., Drossopoulou, S.: Chai: Traits for Java-Like Languages. In: Black, A.P. (ed.) ECOOP 2005. LNCS, vol. 3586, pp. 453–478. Springer, Heidelberg (2005)
Turon, A.: Metaprogramming with Traits. Honors thesis, forthcoming as a University of Chicago technical report (2007)
Author information
Authors and Affiliations
Editor information
Rights and permissions
Copyright information
© 2007 Springer-Verlag Berlin Heidelberg
About this paper
Cite this paper
Reppy, J., Turon, A. (2007). Metaprogramming with Traits. In: Ernst, E. (eds) ECOOP 2007 – Object-Oriented Programming. ECOOP 2007. Lecture Notes in Computer Science, vol 4609. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-540-73589-2_18
Download citation
DOI: https://doi.org/10.1007/978-3-540-73589-2_18
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-540-73588-5
Online ISBN: 978-3-540-73589-2
eBook Packages: Computer ScienceComputer Science (R0)