Generic programming

This is an old revision of this page, as edited by Wile E. Heresiarch (talk | contribs) at 01:45, 11 July 2004 (Reverted edit of 66.130.152.126, changed back to last version by 82.45.232.171). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer science, generics are a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts (so-called subtype) are kept. The programming style with it is called generic programming.

Among OOP languages, C++, D, BETA, Eiffel, Ada, and later versions of Java and C# provide generic facilities. In C++, templates support generics and popularized the notion of generics.

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

Dynamic typing, such 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 the benefits of dynamic typing with the advantages of having static typing.

Uncommon, template metaprogramming is a way of making algorithms evaluate when your code is compiled.



See also Partial evaluation