Encyclopedia > Generics

  Article Content

Generic

Redirected from Generics

In computer science, generics are a technique that allows one value to take different datatype (so-called polymorphism) as long as certain contracts (so-called subtype) are kept. The programming style with it is called "generic programming". With properly implemented generics, it is possible to do Template metaprogramming, which is a way of making algorithms evaluate when your code is compiled.

Among OOP languages, C++, Beta, Eiffel, Ada, and later versions of Java provides generic facility. In C++, templates support generics and popularized the notion of generics.

For example, in C++ code,

template <typename T>
T max (T x, T y)
{
  if (x < y)
    return y;
  else
    return x;
}

In this case, a pseudo-datatype T is called "subtype". T can be anything that can be compared.


See also Partial evaluation



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Quadratic formula

... our quadratic equation by a, we have <math> x^2 + \left( \frac{b}{a} \right) x + \frac{c}{a}=0 </math> which is ...

 
 
 
This page was created in 30.4 ms