Abstract
Traits offer a fine-grained mechanism to compose classes from reusable components while avoiding problems of fragility brought by multiple inheritance and mixins. Traits as originally proposed are stateless, that is, they contain only methods, but no instance variables. State can only be accessed within traits by accessors, which become required methods of the trait. Although this approach works reasonably well in practice, it means that many traits, viewed as software components, are artificially incomplete, and classes that use such traits may contain significant amounts of boilerplate glue code. Although these limitations are largely mitigated by proper tool support, we seek a cleaner solution that supports stateful traits. The key difficulty is how to handle conflicts that arise when composed traits contribute instance variables whose names clash. We present a solution that is faithful to the guiding principle of stateless traits: the client retains control of the composition. Stateful traits consist of a minimal extension to stateless traits in which instance variables are purely local to the scope of a trait, unless they are explicitly made accessible by the composing client of a trait. Naming conflicts are avoided, and variables of disjoint traits can be explicitly merged by clients. We discuss and compare two implementation strategies, and briefly present a case study in which stateful traits have been used to refactor the trait-based version of the Smalltalk collection hierarchy.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Preview
Unable to display preview. Download preview PDF.
References
Bracha, G., Cook, W.: Mixin-based inheritance. In: Proceedings OOPSLA/ECOOP ’90. ACM SIGPLAN Notices, vol. 25, pp. 303–311 (Oct. 1990)
Bak, L., et al.: Mixins in Strongtalk. In: ECOOP ’02 Workshop on Inheritance (June 2002)
Bracha, G.: The Programming Language Jigsaw: Mixins, Modularity and Multiple Inheritance. PhD thesis, Dept. of Computer Science, University of Utah (March 1992)
Black, A.P., Schärli, N., Ducasse, S.: Applying traits to the Smalltalk collection hierarchy. In: Proceedings OOPSLA’03 (International Conference on Object-Oriented Programming Systems, Languages and Applications), vol. 38, October 2003, pp. 47–64 (2003)
Cardelli, L., et al.: Modula-3 language definition. ACM SIGPLAN Notices 27(8), 15–42 (1992)
Chambers, C.: Object-oriented multi-methods in cecil. In: Lehrmann Madsen, O. (ed.) ECOOP 1992. LNCS, vol. 615, pp. 33–56. Springer, Heidelberg (1992)
Ducasse, S., et al.: Traits: A mechanism for fine-grained reuse. ACM Transactions on Programming Languages and Systems 28(2), 331–388 (2006)
The fortress language specification, http://research.sun.com/projects/plrg/fortress0866.pdf
Fisher, K., Reppy, J.: Statically typed traits. Technical Report TR-2003-13, University of Chicago, Department of Computer Science (December 2003)
Ingalls, D., et al.: Back to the future: The story of Squeak, A practical Smalltalk written in itself. In: Proceedings OOPSLA ’97, November 1997. ACM SIGPLAN Notices, pp. 318–326. ACM Press, New York (1997)
The jikes research virtual machine, http://jikesrvm.sourceforge.net/
Krogdahl, S.: Multiple inheritance in simula-like languages. In: BIT 25, pp. 318–326 (1985)
Meyer, B.: Eiffel: The Language. Prentice-Hall, Englewood Cliffs (1992)
Mohnen, M.: Interfaces with default implementations in Java. In: Conference on the Principles and Practice of Programming in Java, Dublin, Ireland, June 2002, pp. 35–40. ACM Press, New York (2002)
Nierstrasz, O., Ducasse, S., Schärli, N.: Flattening Traits. Journal of Object Technology 5(4), 129–148 (2006)
Python, http://www.python.org
Scala home page, http://lamp.epfl.ch/scala/
Smith, C., Drossopoulou, S.: Chai: Typed traits in Java. In: Black, A.P. (ed.) ECOOP 2005. LNCS, vol. 3586, Springer, Heidelberg (2005)
Schärli, N., et al.: Traits: Composable units of behavior. In: Cardelli, L. (ed.) ECOOP 2003. LNCS, vol. 2743, pp. 248–274. Springer, Heidelberg (2003)
Stroustrup, B., Ellis, M.A.: The Annotated C++ Reference Manual. Addison-Wesley, Reading (1990)
Sweeney, P.F., Gil, J.: Space and time-efficient memory layout for multiple inheritance. In: Proceedings OOPSLA ’99, pp. 256–275. ACM Press, New York (1999)
Slate, http://slate.tunes.org
Snyder, A.: Encapsulation and inheritance in object-oriented programming languages. In: Proceedings OOPSLA ’86, November 1986. ACM SIGPLAN Notices, vol. 21, pp. 38–45. ACM, New York (1986)
Ungar, D., et al.: Organizing programs without classes. LISP and SYMBOLIC COMPUTATION: An international journal 4(3) (1991)
Ungar, D., Smith, R.B.: Self: The power of simplicity. In: Proceedings OOPSLA ’87, December 1987. ACM SIGPLAN Notices, vol. 22, pp. 227–242. ACM, New York (1987)
Author information
Authors and Affiliations
Editor information
Rights and permissions
Copyright information
© 2007 Springer Berlin Heidelberg
About this paper
Cite this paper
Bergel, A., Ducasse, S., Nierstrasz, O., Wuyts, R. (2007). Stateful Traits. In: De Meuter, W. (eds) Advances in Smalltalk. ISC 2006. Lecture Notes in Computer Science, vol 4406. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-540-71836-9_4
Download citation
DOI: https://doi.org/10.1007/978-3-540-71836-9_4
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-540-71835-2
Online ISBN: 978-3-540-71836-9
eBook Packages: Computer ScienceComputer Science (R0)