Skip to main content

Language Support for Generic Programming in Object-Oriented Languages: Peculiarities, Drawbacks, Ways of Improvement

  • Conference paper
  • First Online:
  • 1052 Accesses

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

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.

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   39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   54.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

Notes

  1. 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. 2.

    The “&” symbol means that an argument is passed by reference.

  3. 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. 4.

    Similarly to .NET, Kotlin supports extending classes with methods and properties, but interface implementation in extensions is not allowed.

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

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

    Google Scholar 

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

    Google Scholar 

  3. Belyakova, J., Mikhalkovich, S.: Pitfalls of C# generics and their solution using concepts. Proc. Inst. Syst. Program. 27(3), 29–45 (2015)

    Article  Google Scholar 

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

    Google Scholar 

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

    Google Scholar 

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

    Google Scholar 

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

    Article  MATH  Google Scholar 

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

    Article  MATH  Google Scholar 

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

    Google Scholar 

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

    Article  Google Scholar 

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

    Google Scholar 

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

    Article  MATH  Google Scholar 

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

    Chapter  Google Scholar 

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

    Article  MATH  Google Scholar 

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

    Google Scholar 

  16. Pelenitsyn, A.: Associated types and constraint propagation for generic programming in scala. Program. Comput. Softw. 41(4), 224–230 (2015)

    Article  MathSciNet  Google Scholar 

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

    Article  MathSciNet  MATH  Google Scholar 

  18. Stepanov, A.A., Lee, M.: The standard template library. Technical report 95–11(R.1), HP Laboratories, November 1995

    Google Scholar 

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

    Google Scholar 

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

    Google Scholar 

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

    Google Scholar 

  22. Sutton, A.: C++ Extensions for Concepts PDTS. Technical Specification N4377, ISO/IEC JTC1/SC22/WG21, C++ Standards Committee Papers, February 2015

    Google Scholar 

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

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

    Article  Google Scholar 

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

Download references

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

Authors

Corresponding author

Correspondence to Julia Belyakova .

Editor information

Editors and Affiliations

Rights and permissions

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

Publish with us

Policies and ethics