Microsoft Store
 

Generic programming


 

In computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. The programming style emphasizing use of this technique is called generic programming.

Related Topics:
Computer science - Datatype - Polymorphism - Subtype - Signature

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

For example, if one wanted to create a list using generics, a possible declaration would be to say List, where T represented the type. When instantiated, one could create List or List. The list then, is treated as a list of whichever type is specified.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Among object-oriented languages, C++, D, BETA, Eiffel, Ada, and versions of Java (1.5 and higher) provide generic facilities. VB.NET and C# began providing them with .NET 2.0. Much earlier than in all

Related Topics:
Object-oriented - C++ - D - BETA - Eiffel - Ada - Java - VB.NET - C#

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

mentioned languages, generic programming was implemented in CLU.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

It was, however, templates of C++ that popularized the notion of generics.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Templates allow code to be written without consideration of the data type with which it will eventually be used.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Dynamic typing, such as is featured in Objective-C, and, if necessary, judicious use of protocols circumvent the need for use of generic programming techniques, since there exists a general type to contain any object. Whilst Java does so also, the casting that needs to be done breaks the discipline of static typing, and generics are one way of achieving some of the benefits of dynamic typing with the advantages of having static typing.

Related Topics:
Dynamic typing - Objective-C - Protocols - Static typing

~ ~ ~ ~ ~ ~ ~ ~ ~ ~