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
根据发表在 J.函数式编程的考虑1AnExtendedComparativeStudy 的语言支持泛型编程¨RONALDGARCIA1,JAAKKOJARVI2,ANDREWLUMSDAINE1JEREMYSIEK3 JEREMIAHWILLCOCK11 开放系统实验室,印第安纳大学布卢明顿,在美国2 德克萨斯 A & M 大学,计算机科学,大学站,德克萨斯州美国3 水稻大学,计算机科学,休斯顿,德克萨斯州美国(电子邮件: {garcia,lums,jewillco}@osl.iu.edu、 jeremy.g.siek@rice.edu、 jarvi@cs.tamu.edu)摘要许多现代编程语言都支持基本泛型,sufficient 执行类型-safepolymorphiccontainers。Somelanguageshavemovedbeyondthisbasicsupport,andindoingsohaveenabledabroader,morepowerfulformofgenericprogramming。这paperreportsonacomprehensivecomparisonoffacilitiesforgenericprogrammingineight编程语言: C、 标准毫升、 目的 Caml、 哈斯克尔、 Eiffel、 Java、 C#++(withitsproposedgenericsextension),andCecil.Byimplementingasubstantialexample在这些语言中,我们说明了泛型编程的基本角色的可以是 representedineachlanguage。Wealsoidentifyeightlanguageproperties 冰川期thisbroaderviewofgenericprogramming:supportformulti-typeconcepts,multiplecon-straintsontypeparameters,convenientassociated typeaccess constraintsonassociated类型,追溯建模,键入别名,单独编译的算法和数据结构和泛型算法的隐式参数类型推导。我们要做的thesefeaturesarenecessarytoavoidawkwarddesigns,poormaintainability andpainfully冗长的代码。作为语言越来越支持泛型,它是重要的语言designersunderstandthefeaturesnecessarytoenabletheeffectiveuseofgenericsandthattheirabsencecancausedifficultiesforprogrammers。内容1 引言2 泛型编程243 通用图形库8在 c + + 4 图形库1314171718212427294.14.2执行EvaluationofC + +在标准毫升 5 图形库5.15.2执行EvaluationofStandardML在 OCaml 6 图形库6.16.2执行EvaluationofOCaml 2Garciaetal。7 图图书馆在 Haskell 中3131333435364040424343454646494950525254565758607.17.2执行EvaluationofHaskellEiffel 8 图库8.18.2执行EvaluationofEiffel在 Java 中 9 图形库9.19.2执行EvaluationofJavaC# 泛型中的 10Graph 图书馆10.1 执行10.2 #generics EvaluationofC11Graph 库中塞西尔 ·11.1 执行11.2 EvaluationofCecil12Discussion12.1 EncapsulatingTypeConstraintsinConcepts12.2 AccesstoAssociatedTypes12.3 ImplicitInstantiation12.4 EstablishingtheModelingRelation12.5 TypeAliases12.6 ConciseSyntax13Conclusion: Beyondfold andList引用1 引言Genericsareanincreasinglypopularandimportanttool 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
正在翻譯中..
![](//zhcntimg.ilovetranslation.com/pic/loading_3.gif?v=b9814dd30c1d7c59_8619)