Under consideration for publication in J. Functional Programming
1
AnExtendedComparativeStudy of Language
Supportfor Generic Programming
¨
RONALDGARCIA1,JAAKKOJARVI2,ANDREWLUMSDAINE1
JEREMYSIEK3,JEREMIAHWILLCOCK1
1 Open Systems Lab, Indiana University, Bloomington, IN USA
2 Texas A&M University, Computer Science, College Station, TX USA
3 Rice University, Computer Science, Houston, TX USA
(e-mail: {garcia,lums,jewillco}@osl.iu.edu, jeremy.g.siek@rice.edu, jarvi@cs.tamu.edu)
Abstract
Many modern programming languages support basic generics, sufficient to implement
type-safepolymorphiccontainers.Somelanguageshavemovedbeyondthisbasicsupport,
andindoingsohaveenabledabroader,morepowerfulformofgenericprogramming.This
paperreportsonacomprehensivecomparisonoffacilitiesforgenericprogrammingineight
programming languages: C , Standard ML, Objective Caml, Haskell, Eiffel, Java, C#
++
(withitsproposedgenericsextension),andCecil.Byimplementingasubstantialexample
in each of these languages, we illustrate how the basic roles of generic programming can
be representedineachlanguage.Wealsoidentifyeightlanguageproperties thatsupport
thisbroaderviewofgenericprogramming:supportformulti-typeconcepts,multiplecon-
straintsontypeparameters,convenientassociated typeaccess,constraintsonassociated
types, retroactive modeling, type aliases, separate compilation of algorithms and data
structures, and implicit argument type deduction for generic algorithms. We find that
thesefeaturesarenecessarytoavoidawkwarddesigns,poormaintainability,andpainfully
verbose code. As languages increasingly support generics, it is important that language
designersunderstandthefeaturesnecessarytoenabletheeffectiveuseofgenericsandthat
theirabsencecancausedifficultiesforprogrammers.
Contents
1 Introduction
2 Generic Programming
2
4
3 A Generic Graph Library
8
4 Graph Library in C++
13
14
17
17
18
21
24
27
29
4.1
4.2
Implementation
EvaluationofC++
5 Graph Library in Standard ML
5.1
5.2
Implementation
EvaluationofStandardML
6 Graph Library in OCaml
6.1
6.2
Implementation
EvaluationofOCaml
2
Garciaetal.
7 Graph Library in Haskell
31
31
33
34
35
36
40
40
42
43
43
45
46
46
49
49
50
52
52
54
56
57
58
60
7.1
7.2
Implementation
EvaluationofHaskell
8 Graph Library in Eiffel
8.1
8.2
Implementation
EvaluationofEiffel
9 Graph Library in Java
9.1
9.2
Implementation
EvaluationofJava
10Graph Library in C# generics
10.1 Implementation
10.2 EvaluationofC#generics
11Graph Library in Cecil
11.1 Implementation
11.2 EvaluationofCecil
12Discussion
12.1 EncapsulatingTypeConstraintsinConcepts
12.2 AccesstoAssociatedTypes
12.3 ImplicitInstantiation
12.4 EstablishingtheModelingRelation
12.5 TypeAliases
12.6 ConciseSyntax
13Conclusion: Beyondfold andList
References
1 Introduction
Genericsareanincreasinglypopularandimportanttool forsoftwaredevelopment
and many modern programming languages provide basic support for them. For
example,theuseoftype-safepolymorphiccontainersisroutineprogrammingprac-
tice today. Some languages have moved beyond elementary generics to support-
ing a broader, more powerful form of generic programming, enabling the devel-
opment of highly reusable algorithms. Such extensive support for generics has
proven valuable in practice. Generic programming has been a particularly effec-
tivemeansforconstructingreusablelibrariesofsoftwarecomponents,oneexample
ofwhichistheStandardTemplateLibrary(STL),nowpartoftheC++
Standard
Library(Stepanov&Lee,1994;ISO1998).Asthegenericprogrammingparadigm
gainsmomentum,theneedforaclearanddeepunderstandingofthelanguageis-
suesincreases.Inparticular,itisimportanttounderstandwhatlanguagefeatures
arerequiredtosupportthisbroadernotionofgenericprogramming.
To aid in this process, we present results of a study comparing eight program-
ming languages that support generics: Standard ML (Milner et al., 1997), Objec-
tive Caml (Leroy, 2000), C++ (ISO, 1998), Haskell (Peyton Jones et al. , 1999),
LanguageSupportforGenericProgramming
3
Eiffel(Meyer,1992),Java(Goslingetal.,2005),C#(Kennedy&Syme,2001;Mi-
crosoft Corporation, 2002), and Cecil (Litvinov, 1998). Seven of these languages
currentlysupportgenerics,andtheyhavebeenimplementedandproposedforthe
nextrevisionofC#.Theselanguageswereselectedbecausetheyarewidelyusedor
representthestateoftheartinprogramminglanguageswithgenerics.Thispaperis
arevisedandextendedversionof(Garciaetal.,2003),featuringupdatedanalyses
andtheadditionoftwolanguages,ObjectiveCamlandCecil.
Ourgoalsforthisstudywerethefollowing:
• Tounderstandwhichlanguagefeaturesarenecessarytosupportgenericpro-
gramming;
• To understand the extent to which specific languages support generic pro-
gramming;
• Toprovideguidancefordevelopmentoflanguagesupportforgenerics;and
• Toilluminateforthecommunitysomeofthepowerandsubtletiesofgeneric
programming.
Itisdecidedlynotagoalofthispapertodemonstratethatanyonelanguageis“bet-
ter”thananyother.Thispaperisalsonotacomparisonofgenericprogrammingto
anyotherprogrammingparadigm,beitobject-oriented,f
Under consideration for publication in J. Functional Programming1AnExtendedComparativeStudy of LanguageSupportfor Generic Programming¨RONALDGARCIA1,JAAKKOJARVI2,ANDREWLUMSDAINE1JEREMYSIEK3,JEREMIAHWILLCOCK11 Open Systems Lab, Indiana University, Bloomington, IN USA2 Texas A&M University, Computer Science, College Station, TX USA3 Rice University, Computer Science, Houston, TX USA(e-mail: {garcia,lums,jewillco}@osl.iu.edu, jeremy.g.siek@rice.edu, jarvi@cs.tamu.edu)AbstractMany modern programming languages support basic generics, sufficient to implementtype-safepolymorphiccontainers.Somelanguageshavemovedbeyondthisbasicsupport,andindoingsohaveenabledabroader,morepowerfulformofgenericprogramming.Thispaperreportsonacomprehensivecomparisonoffacilitiesforgenericprogrammingineightprogramming languages: C , Standard ML, Objective Caml, Haskell, Eiffel, Java, C#++(withitsproposedgenericsextension),andCecil.Byimplementingasubstantialexamplein each of these languages, we illustrate how the basic roles of generic programming canbe representedineachlanguage.Wealsoidentifyeightlanguageproperties thatsupportthisbroaderviewofgenericprogramming:supportformulti-typeconcepts,multiplecon-straintsontypeparameters,convenientassociated typeaccess,constraintsonassociatedtypes, retroactive modeling, type aliases, separate compilation of algorithms and datastructures, and implicit argument type deduction for generic algorithms. We find thatthesefeaturesarenecessarytoavoidawkwarddesigns,poormaintainability,andpainfullyverbose code. As languages increasingly support generics, it is important that languagedesignersunderstandthefeaturesnecessarytoenabletheeffectiveuseofgenericsandthattheirabsencecancausedifficultiesforprogrammers.Contents1 Introduction2 Generic Programming243 A Generic Graph Library84 Graph Library in C++1314171718212427294.14.2ImplementationEvaluationofC++5 Graph Library in Standard ML5.15.2ImplementationEvaluationofStandardML6 Graph Library in OCaml6.16.2ImplementationEvaluationofOCaml 2Garciaetal.7 Graph Library in Haskell3131333435364040424343454646494950525254565758607.17.2ImplementationEvaluationofHaskell8 Graph Library in Eiffel8.18.2ImplementationEvaluationofEiffel9 Graph Library in Java9.19.2ImplementationEvaluationofJava10Graph Library in C# generics10.1 Implementation10.2 EvaluationofC#generics11Graph Library in Cecil11.1 Implementation11.2 EvaluationofCecil12Discussion12.1 EncapsulatingTypeConstraintsinConcepts12.2 AccesstoAssociatedTypes12.3 ImplicitInstantiation12.4 EstablishingtheModelingRelation12.5 TypeAliases12.6 ConciseSyntax13Conclusion: Beyondfold andListReferences1 IntroductionGenericsareanincreasinglypopularandimportanttool forsoftwaredevelopmentand many modern programming languages provide basic support for them. Forexample,theuseoftype-safepolymorphiccontainersisroutineprogrammingprac-tice today. Some languages have moved beyond elementary generics to support-ing a broader, more powerful form of generic programming, enabling the devel-opment of highly reusable algorithms. Such extensive support for generics hasproven valuable in practice. Generic programming has been a particularly effec-tivemeansforconstructingreusablelibrariesofsoftwarecomponents,oneexampleofwhichistheStandardTemplateLibrary(STL),nowpartoftheC++StandardLibrary(Stepanov&Lee,1994;ISO1998).Asthegenericprogrammingparadigmgainsmomentum,theneedforaclearanddeepunderstandingofthelanguageis-suesincreases.Inparticular,itisimportanttounderstandwhatlanguagefeaturesarerequiredtosupportthisbroadernotionofgenericprogramming.To aid in this process, we present results of a study comparing eight program-ming languages that support generics: Standard ML (Milner et al., 1997), Objec-tive Caml (Leroy, 2000), C++ (ISO, 1998), Haskell (Peyton Jones et al. , 1999), LanguageSupportforGenericProgramming3Eiffel(Meyer,1992),Java(Goslingetal.,2005),C#(Kennedy&Syme,2001;Mi-crosoft Corporation, 2002), and Cecil (Litvinov, 1998). Seven of these languagescurrentlysupportgenerics,andtheyhavebeenimplementedandproposedforthenextrevisionofC#.Theselanguageswereselectedbecausetheyarewidelyusedorrepresentthestateoftheartinprogramminglanguageswithgenerics.Thispaperisarevisedandextendedversionof(Garciaetal.,2003),featuringupdatedanalysesandtheadditionoftwolanguages,ObjectiveCamlandCecil.Ourgoalsforthisstudywerethefollowing:• Tounderstandwhichlanguagefeaturesarenecessarytosupportgenericpro-gramming;• To understand the extent to which specific languages support generic pro-gramming;• Toprovideguidancefordevelopmentoflanguagesupportforgenerics;and• Toilluminateforthecommunitysomeofthepowerandsubtletiesofgenericprogramming.Itisdecidedlynotagoalofthispapertodemonstratethatanyonelanguageis“bet-ter”thananyother.Thispaperisalsonotacomparisonofgenericprogrammingtoanyotherprogrammingparadigm,beitobject-oriented,f
正在翻譯中..