Skip to main content

Metaprogramming with Traits

  • Conference paper

Part of the book series: Lecture Notes in Computer Science ((LNPSE,volume 4609))

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.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   84.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   109.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  1. Ancona, D., Damiani, F., Drossopoulou, S., Zucca, E.: Polymorphic bytecode: compositional compilation for Java-like languages. In: POPL’05, pp. 26–37 (2005)

    Google Scholar 

  2. Ancona, D., Lagorio, G., Zucca, E.: Jam–designing a Java extension with mixins. TOPLAS 25(5), 641–712 (2003)

    Article  Google Scholar 

  3. Bono, V., Bugliesi, M., Liquori, L.: A Lambda Calculus of Incomplete Objects. In: MFCS, pp. 218–229 (1996)

    Google Scholar 

  4. Bracha, G.: The Programming Language Jigsaw: Mixins, Modularity and Multiple Inheritance. PhD thesis, University of Utah (March 1992)

    Google Scholar 

  5. Bracha, G., Cook, W.: Mixin-based inheritance. In: ECOOP’90, October 1990, pp. 303–311. ACM Press, New York (1990)

    Google Scholar 

  6. Czarnecki, K., Eisenecker, U.W.: Generative programming: methods, tools, and applications. ACM Press/Addison-Wesley Publishing Co, New York (2000)

    Google Scholar 

  7. 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)

    Google Scholar 

  8. 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)

    Chapter  Google Scholar 

  9. Ducasse, S., Nierstrasz, O., Schärli, N., Wuyts, R., Black, A.: Traits: A Mechanism for fine-grained Reuse. TOPLAS 28(2), 331–388 (2006)

    Article  Google Scholar 

  10. Fähndrich, M., Carbin, M., Larus, J.R.: Reflective program generation with patterns. In: GPCE’06, pp. 275–284. ACM Press, New York (2006)

    Google Scholar 

  11. Fisher, K., Reppy, J.: Statically typed traits. Technical Report TR-2003-13, Dept. of Computer Science, U. of Chicago, Chicago, IL (December 2003)

    Google Scholar 

  12. Fisher, K., Reppy, J.: A typed calculus of traits. In: FOOL11 (January 2004)

    Google Scholar 

  13. 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)

    Chapter  Google Scholar 

  14. 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)

    Chapter  Google Scholar 

  15. Hofmann, M.: Syntax and Semantics of Dependent Types. In: Semantics and Logics of Computation, vol. 14, pp. 79–130. Cambridge University Press, Cambridge (1997)

    Google Scholar 

  16. 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)

    Chapter  Google Scholar 

  17. Igarashi, A., Pierce, B.C., Wadler, P.: Featherweight Java: a minimal core calculus for Java and GJ. TOPLAS 23(3), 396–450 (2001)

    Article  Google Scholar 

  18. Kiczales, G., des Rivieres, J., Bobrow, D.G.: The art of metaobject protocol. MIT Press, Cambridge, MA (1991)

    Google Scholar 

  19. 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)

    Chapter  Google Scholar 

  20. Liquori, L., Spiwack, A.: Feathertrait: A modest extension of featherweight java. TOPLAS (to appear, 2007)

    Google Scholar 

  21. Malayeri, D., Aldrich, J.: Combining Structural Subtyping and External Dispatch. In: FOOL/WOOD’07 (2007)

    Google Scholar 

  22. Nierstrasz, O., Ducasse, S., Schärli, N.: Flattening Traits. Journal of Object Technology 5(4), 129–148 (2006)

    Google Scholar 

  23. 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)

    Google Scholar 

  24. Quitslund, P.J.: Java traits — improving opportunities for reuse. Technical Report CSE 04-005, OGI School of Science & Engineering (September 2004)

    Google Scholar 

  25. Reppy, J., Turon, A.: A foundation for trait-based metaprogramming. In: FOOL/WOOD’06 (2006)

    Google Scholar 

  26. Riecke, J.G., Stone, C.A.: Privacy via subsumption. INC (A preliminary version appeared in FOOL5) 172(1), 2–28 (2002)

    MATH  MathSciNet  Google Scholar 

  27. 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)

    Google Scholar 

  28. Turon, A.: Metaprogramming with Traits. Honors thesis, forthcoming as a University of Chicago technical report (2007)

    Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Editor information

Erik Ernst

Rights and permissions

Reprints 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)

Publish with us

Policies and ethics