Generic programming: Difference between revisions

Content deleted Content added
m Add inline citation. Add link to JVM.
Remove duplicate references, use {{rp}} instead.
Line 377:
{{Main article|Generics in Java}}
 
Support for the ''generics'', or "containers-of-type-T" was added to the [[Java (programming language)|Java programming language]] in 2004 as part of J2SE 5.0. In Java, generics are only checked at compile time for type correctness. The generic type information is then removed via a process called [[type erasure]], to maintain compatibility with old [[Java virtual machine|JVM]] implementations, making it unavailable at runtime.<ref name=Bloch>''{{cite book | title= "Effective Java: Programming Language Guide''," third|last=Bloch| first=Joshua| publisher=Addison-Wesley | edition:=third {{ISBN| isbn=978-0134685991}},| year=2018, p.126}}</ref>{{rp|126}} For example, a <code>List&lt;String&gt;</code> is converted to the raw type <code>List</code>. The compiler inserts [[Type conversion|type casts]] to convert the elements to the <code>String</code> type when they are retrieved from the list, reducing performance compared to other implementations such as C++ templates.
 
====Genericity in .NET [C#, VB.NET]====
 
Generics were added as part of [[.NET Framework#.NET Framework 2.0|.NET Framework 2.0]] in November 2005, based on a research prototype from Microsoft Research started in 1999.<ref>[https://docs.microsoft.com/en-us/archive/blogs/dsyme/netc-generics-history-some-photos-from-feb-1999 .NET/C# Generics History: Some Photos From Feb 1999]</ref> Although similar to generics in Java, .NET generics do not apply [[type erasure]]<ref name=Albahari>{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |page= 208-209 |isbn= 978-1-098-12195-2}}</ref>{{rp|208-209}}, but implement generics as a first class mechanism in the runtime using [[Reification (computer science)|reification]]. This design choice provides additional functionality, such as allowing [[Reflection (computer science)|reflection]] with preservation of generic types, as well as alleviating some of the limitations of erasure (such as being unable to create generic arrays).<ref>[https://www.ondotnet.com/pub/a/dotnet/2005/10/17/interview-with-anders-hejlsberg.html C#: Yesterday, Today, and Tomorrow: An Interview with Anders Hejlsberg]</ref><ref>[https://www.artima.com/intv/generics2.html Generics in C#, Java, and C++]</ref> This also means that there is no performance hit from runtime [[Type conversion|casts]] and normally expensive [[Boxing (computer science)|boxing conversions]]. When primitive and value types are used as generic arguments, they get specialized implementations, allowing for efficient generic [[Collection class|collections]] and methods. As in C++ and Java, nested generic types such as Dictionary<string, List<int>> are valid types, however are advised against for member signatures in code analysis design rules.<ref>[https://msdn.microsoft.com/en-us/library/ms182144.aspx Code Analysis CA1006: Do not nest generic types in member signatures]</ref>
 
.NET allows six varieties of generic type constraints using the <code>where</code> keyword including restricting generic types to be value types, to be classes, to have constructors, and to implement interfaces.<ref>[https://msdn2.microsoft.com/en-us/library/d5x73970.aspx Constraints on Type Parameters (C# Programming Guide)]</ref> Below is an example with an interface constraint:
Line 692:
== Sources ==
{{refbegin}}
* {{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |publisher= O'Reilly |page= 208-209 |isbn= 978-1-098-12195-2}}
* {{Cite book | last1 = Musser | first1 = D. R. | author-link1 = David Musser | last2 = Stepanov | first2 = A. A. | author-link2 = Alexander Stepanov| chapter = Generic programming | doi = 10.1007/3-540-51084-2_2 | title = Symbolic and Algebraic Computation: International symposium ISSAC 1988 | editor = P. Gianni | editor-link = Patrizia Gianni | series = Lecture Notes in Computer Science | volume = 358 | pages = 13–25 | year = 1989 | isbn = 978-3-540-51084-0 }}
* {{cite conference | url =https://www.research.att.com/~bs/hopl-almost-final.pdf |title=Evolving a language in and for the real world: C++ 1991-2006 |author-link=Bjarne Stroustrup |last=Stroustrup |first=Bjarne |conference=ACM HOPL 2007 |year=2007}}