Abstract
Earlier comparative studies of language support for generic programming (GP) have shown that mainstream object-oriented (OO) languages such as C# and Java provide weaker support for GP as compared with functional languages such as Haskell or SML. But many new object-oriented languages have appeared in recent years. Have they improved the support for generic programming? And if not, is there a reason why OO languages yield to functional ones in this respect? In this paper we analyse language constructs for GP in seven modern object-oriented languages. We demonstrate that all of these languages follow the same approach to constraining type parameters, which has a number of inevitable problems. However, those problems are successfully lifted with the use of the another approach. Several language extensions that adopt this approach and allow to improve GP in OO languages are considered. We analyse the dependencies between different language features, discuss the features’ support using both approaches, and propose which approach is more expressive.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Some details were omitted for simplicity. To make the code correct, one has to add \({\small \texttt {\#[derive(Debug,Copy,Clone)]}}\) before the \({\small \texttt {Point}}\) definition.
- 2.
The “&” symbol means that an argument is passed by reference.
- 3.
Some details were omitted for simplicity. The following declaration is to be provided to make the code correct: \({\small \texttt {\#[derive(Copy, Clone)]}}\) before the definition \({\small \texttt {struct Pair<S : Copy, T : Copy>}}\). Yet the type parameters of the \({\small \texttt {impl}}\) for pair must be constrained with \({\small \texttt {Copy+Equatable}}\).
- 4.
Similarly to .NET, Kotlin supports extending classes with methods and properties, but interface implementation in extensions is not allowed.
- 5.
The Concept design pattern can also be used, but it has its own drawbacks. We will discuss concept pattern later, in Sect. 2.2.
- 6.
Scala is often blamed for its complex rules of implicits resolution: sometimes it is not clear which implicit object is to be used.
- 7.
The default model can be generated automatically for a type if the type conforms to a concept, i. e. it provides methods required by the concept.
References
Belyakova, J., Mikhalkovich, S.: A support for generic programming in the modern object-oriented languages. Part 1. Anal. Probl. 2(2), 63–77 (2015). Transactions of Scientific School of I.B. Simonenko (in Russian)
Belyakova, J., Mikhalkovich, S.: A support for generic programming in the modern object-oriented languages. Part 2. Rev. Mod. Solutions 2(2), 78–92 (2015). Transactions of Scientific School of I.B. Simonenko (in Russian)
Belyakova, J., Mikhalkovich, S.: Pitfalls of C# generics and their solution using concepts. Proc. Inst. Syst. Program. 27(3), 29–45 (2015)
Bernardy, J.P., Jansson, P., Zalewski, M., Schupp, S., Priesnitz, A.: A comparison of C++ concepts and haskell type classes. In: Proceedings of the ACM SIGPLAN Workshop on Generic Programming, WGP 2008, New York, NY, USA, pp. 37–48. ACM (2008)
Bruce, K., Cardelli, L., Castagna, G., Leavens, G.T., Pierce, B.: On binary methods. Theor. Pract. Object Syst. 1(3), 221–242 (1995). http://dl.acm.org/citation.cfm?id=230849.230854
Dos Reis, G., Stroustrup, B.: Specifying C++ concepts. In: Conference Record of the 33rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 2006, New York, NY, USA, pp. 295–308. ACM (2006)
Garcia, R., Jarvi, J., Lumsdaine, A., Siek, J., Willcock, J.: An extended comparative study of language support for generic programming. J. Funct. Program. 17(2), 145–205 (2007)
Garcia, R., Jarvi, J., Lumsdaine, A., Siek, J.G., Willcock, J.: A comparative study of language support for generic programming. SIGPLAN Not. 38(11), 115–134 (2003). http://doi.acm.org/10.1145/949343.949317
Greenman, B., Muehlboeck, F., Tate, R.: Getting F-bounded polymorphism into shape. In: Proceedings of the 35th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2014, New York, NY, USA, pp. 89–99. ACM (2014)
Hall, C.V., Hammond, K., Peyton Jones, S.L., Wadler, P.L.: Type classes in haskell. ACM Trans. Program. Lang. Syst. 18(2), 109–138 (1996). http://doi.acm.org/10.1145/227699.227700
Järvi, J., Willcock, J., Lumsdaine, A.: Associated types and constraint propagation for mainstream object-oriented generics. In: Proceedings of the 20th Annual ACM SIGPLAN Conference on Object-oriented Programming, Systems, Languages, and Applications, OOPSLA 2005, New York, NY, USA, pp. 1–19. ACM (2005)
Martelli, A., Montanari, U.: An efficient unification algorithm. ACM Trans. Program. Lang. Syst. 4(2), 258–282 (1982). http://doi.acm.org/10.1145/357162.357169
Musser, D.R., Stepanov, A.A.: Generic programming. In: Gianni, P. (ed.) ISSAC 1988. LNCS, vol. 358, pp. 13–25. Springer, Heidelberg (1989). http://dl.acm.org/citation.cfm?id=646361.690581
Oliveira, B.C., Gibbons, J.: Scala for generic programmers: comparing haskell and scala support for generic programming. J. Funct. Program. 20(3–4), 303–352 (2010)
Oliveira, B.C., Moors, A., Odersky, M.: Type classes as objects and implicits. In: Proceedings of the ACM International Conference on Object Oriented Programming Systems Languages and Applications, OOPSLA 2010, New York, NY, USA, pp. 341–360. ACM (2010)
Pelenitsyn, A.: Associated types and constraint propagation for generic programming in scala. Program. Comput. Softw. 41(4), 224–230 (2015)
Siek, J.G., Lumsdaine, A.: A language for generic programming in the large. Sci. Comput. Program. 76(5), 423–465 (2011). http://dx.doi.org/10.1016/j.scico.2008.09.009
Stepanov, A.A., Lee, M.: The standard template library. Technical report 95–11(R.1), HP Laboratories, November 1995
Stroustrup, B.: Concept checking – a more abstract complement to type checking. Technical report N1510=03-0093, ISO/IEC JTC1/SC22/WG21, C++ Standards Committee Papers, October 2003
Stroustrup, B., Dos Reis, G.: Concepts – design choices for template argument checking. Technical report N1522=03-0105, ISO/IEC JTC1/SC22/WG21, C++ Standards Committee Papers, October 2003
Stroustrup, B., Sutton, A.: A concept design for the STL. Technical report N3351=12-0041, ISO/IEC JTC1/SC22/WG21, C++ Standards Committee Papers, January 2012
Sutton, A.: C++ Extensions for Concepts PDTS. Technical Specification N4377, ISO/IEC JTC1/SC22/WG21, C++ Standards Committee Papers, February 2015
Wadler, P., Blott, S.: How to make ad-hoc polymorphism less ad hoc. In: Proceedings of the 16th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL 1989, New York, NY, USA, pp. 60–76. ACM (1989). http://doi.acm.org/10.1145/75277.75283
Wehr, S., Thiemann, P.: JavaGI: the interaction of type classes with interfaces and inheritance. ACM Trans. Program. Lang. Syst. 33(4), 12:1–12:83 (2011). http://doi.acm.org/10.1145/1985342.1985343
Zhang, Y., Loring, M.C., Salvaneschi, G., Liskov, B., Myers, A.C.: Lightweight, flexible object-oriented generics. In: Proceedings of the 36th ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2015, New York, NY, USA, pp. 436–445. ACM (2015). http://doi.acm.org/10.1145/2737924.2738008
Acknowledgment
The authors would like to thank Artem Pelenitsyn, Jeremy Siek, and Ross Tate for helpful discussions on generic programming.
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2016 Springer International Publishing Switzerland
About this paper
Cite this paper
Belyakova, J. (2016). Language Support for Generic Programming in Object-Oriented Languages: Peculiarities, Drawbacks, Ways of Improvement. In: Castor, F., Liu, Y. (eds) Programming Languages. SBLP 2016. Lecture Notes in Computer Science(), vol 9889. Springer, Cham. https://doi.org/10.1007/978-3-319-45279-1_1
Download citation
DOI: https://doi.org/10.1007/978-3-319-45279-1_1
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-45278-4
Online ISBN: 978-3-319-45279-1
eBook Packages: Computer ScienceComputer Science (R0)