A Comparative Study of Language Support for Generic Programming

A Comparative Study of Language Support for Generic Programming

A Comparative Study of Language Support for Generic Programming Ronald Garcia Jaakko J¨arvi Andrew Lumsdaine Jeremy Siek Jeremiah Willcock Open Systems Lab Indiana University Bloomington Bloomington, IN USA {garcia,jajarvi,lums,jsiek,jewillco}@osl.iu.edu ABSTRACT 1. INTRODUCTION Many modern programming languages support basic generic pro- Generic programming is an increasingly popular and important gramming, sufficient to implement type-safe polymorphic contain- paradigm for software development and many modern program- ers. Some languages have moved beyond this basic support to ming languages provide basic support for it. For example, the use a broader, more powerful interpretation of generic programming, of type-safe polymorphic containers is routine programming prac- and their extensions have proven valuable in practice. This pa- tice today. Some languages have moved beyond elementary gener- per reports on a comprehensive comparison of generics in six pro- ics to a broader, more powerful interpretation, and their extensions gramming languages: C++, Standard ML, Haskell, Eiffel, Java (with have proven valuable in practice. One domain where generic pro- its proposed generics extension), and Generic C#. By implement- gramming has been particularly effective is reusable libraries of ing a substantial example in each of these languages, we identify software components, an example of which is the Standard Tem- eight language features that support this broader view of generic plate Library (STL), now part of the C++ Standard Library [23, 45]. programming. We find these features are necessary to avoid awk- As the generic programming paradigm gains momentum, it is im- ward designs, poor maintainability, unnecessary run-time checks, portant to clearly and deeply understand the language issues. In and painfully verbose code. As languages increasingly support particular, it is important to understand what language features are generics, it is important that language designers understand the fea- required to support the broader notion of generic programming. tures necessary to provide powerful generics and that their absence To aid in this process, we present results of an in-depth study causes serious difficulties for programmers. comparing six programming languages that support generics: Stan- dard ML [35], C++ [17], Haskell [21], Eiffel [30], Java (with the pro- posed genericity extension) [6], and Generic C# [24, 33]. The first Categories and Subject Descriptors four currently support generics while the latter two have proposed extensions (and prototype implementations) that do so. These lan- D.2.13 [Software Engineering]: Reusable Software—reusable li- guages were selected because they are widely used and represent braries; D.3.2 [Programming Languages]: Language Classifica- the state of the art in programming languages with generics. tions—multiparadigm languages; D.3.3 [Programming Langua- Our high-level goals for this study were the following: ges]: Language Constructs and Features—abstract data types, con- straints, polymorphism • Understand what language features are necessary to support generic programming; General Terms • Understand the extent to which specific languages support generic programming; Languages, Design, Standardization • Provide guidance for development of language support for generics; and Keywords • Illuminate for the community some of the power and sub- generics, generic programming, polymorphism, C++, Standard ML, tleties of generic programming. Haskell, Eiffel, Java, C# It is decidedly not a goal of this paper to demonstrate that one lan- guage is “better” than any other language. This paper is also not a comparison of generic programming to object-oriented program- Permission to make digital or hard copies of all or part of this work for ming (or to any other paradigm). personal or classroom use is granted without fee provided that copies are To conduct the study, we designed a model library by extract- not made or distributed for profit or commercial advantage and that copies ing a small but significant example of generic programming from a bear this notice and the full citation on the first page. To copy otherwise, to state-of-the art generic library (the Boost Graph Library [41]). The republish, to post on servers or to redistribute to lists, requires prior specific model library was fully implemented in all six target languages. permission and/or a fee. OOPSLA’03, October 26–30, 2003, Anaheim, California, USA. This example was chosen because it includes a variety of generic Copyright 2003 ACM 1-58113-712-5/03/0010 ...$5.00. programming techniques (some beyond the scope of, say, the STL) 1 115 and could therefore expose many subtleties of generic program- Generic programming is a sub-discipline of computer science ming. We attempted to create a uniform implementation across that deals with finding abstract representations of efficient al- all of the languages while still using the standard techniques and gorithms, data structures, and other software concepts, and idioms of each language. For each implementation, we evaluated with their systematic organization. The goal of generic pro- the language features available to realize different facets of generic gramming is to express algorithms and data structures in a programming. In addition, we evaluated each implementation with broadly adaptable, interoperable form that allows their direct respect to software quality issues that generic programming en- use in software construction. Key ideas include: ables, such as modularity, safety, and conciseness of expression. • The results of this process constitute the main results of this pa- Expressing algorithms with minimal assumptions about per and are summarized in Table 1. The table lists the eight lan- data abstractions, and vice versa, thus making them as guage features that we identified as being important to generic pro- interoperable as possible. gramming and shows the level of support for that feature in each • Lifting of a concrete algorithm to as general a level as language. We find these features are necessary for the development possible without losing efficiency; i.e., the most abstract of high-quality generic libraries. Incomplete support of these fea- form such that when specialized back to the concrete tures can result in awkward designs, poor maintainability, unnec- case the result is just as efficient as the original algo- essary run-time checks, and painfully verbose code. As languages rithm. increasingly support generics, it is important that language design- ers understand the features necessary to provide powerful generics • When the result of lifting is not general enough to cover and that their absence causes serious difficulties for programmers. all uses of an algorithm, additionally providing a more The rest of this paper describes how we reached the conclusions general form, but ensuring that the most efficient spe- in the table and why those language properties are important. The cialized form is automatically chosen when applicable. paper is organized as follows. Section 2 provides a brief introduc- • tion to generic programming and defines the terminology we use Providing more than one generic algorithm for the same in the paper. Section 3 describes the design of the generic graph li- purpose and at the same level of abstraction, when none brary that forms the basis for our comparisons. Sections 4 through 9 dominates the others in efficiency for all inputs. This present the individual implementations of the graph library in the introduces the necessity to provide sufficiently precise selected languages. Each of these sections also evaluates the level characterizations of the domain for which each algo- of support for generic programming provided by each language. rithm is the most efficient. In Section 10 we discuss in detail the most important issues we Figure 1: Definition of Generic Programming encountered during the course of this study and provide a detailed explanation of Table 1. We present some conclusions in Section 11. concepts are used to constrain type parameters. Traditionally, a concept consists of associated types, valid ex- 2. GENERIC PROGRAMMING pressions, semantic invariants, and complexity guarantees. The Definitions of generic programming vary. Typically, generic pro- associated types of a concept specify mappings from the model- gramming involves type parameters for data types and functions. ing type to other collaborating types (see Figure 4 for an example). While it is true that type parameters are required for generic pro- Valid expressions specify the operations that must be implemented gramming, there is much more to generic programming than just for the modeling type. At this point in the state of the art, type sys- type parameters. Inspired by the STL, we take a broader view of tems typically do not include semantic invariants and complexity generic programming and use the definition from [18] reproduced guarantees. Therefore, we state that for a type to properly model a in Figure 1. concept, the associated types and valid expressions specified by the Associated with this definition, terminology and techniques for concept must be defined. carrying out generic programming (and for supporting these key These primary aspects of generic programming, i.e., generic al- ideas) have emerged. gorithms, concepts, refinement, modeling, and constraints, are re- alized in different ways in our different target programming lan- Terminology guages. The specific language features that are

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us