Generics is an actively used design pattern created in 1973. Generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by ML in 1973, permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication. Such software entities are known as generics in Python, Ada, C#, Delphi, Eiffel, F#, Java, Rust, Swift, TypeScript and Visual Basic .NET. Read more on Wikipedia...

46Years Old
  • Generics ranks in the top 25% of entities I track
  • the Generics wikipedia page
  • Generics first appeared in 1973
  • Have a question about Generics not answered here? Email me and let me know how I can help.

Languages with Generics include ada, csharp, felix, java, typescript

Example from ada:

   Max_Size : Natural; -- a generic formal value
   type Element_Type is private; -- a generic formal type; accepts any nonlimited type
package Stacks is
   type Size_Type is range 0 .. Max_Size;
   type Stack is limited private;
   procedure Create (S : out Stack;
                     Initial_Size : in Size_Type := Max_Size);
   procedure Push (Into : in out Stack; Element : in Element_Type);
   procedure Pop (From : in out Stack; Element : out Element_Type);
   Overflow : exception;
   Underflow : exception;
   subtype Index_Type is Size_Type range 1 .. Max_Size;
   type Vector is array (Index_Type range <>) of Element_Type;
   type Stack (Allocated_Size : Size_Type := 0) is record
      Top : Index_Type;
      Storage : Vector (1 .. Allocated_Size);
   end record;
end Stacks;

Example from csharp:

// Declare the generic class.
public class GenericList<T>
    public void Add(T input) { }
class TestGenericList
    private class ExampleClass { }
    static void Main()
        // Declare a list of type int.
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string.
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass.
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
        list3.Add(new ExampleClass());

Example from felix:

// generics
fun g (x) => f (f x);
println$ g 1, g "hello";
println$ _map f (1,"hello",2.0);

Example from java:

List<String> v = new ArrayList<String>();
Integer i = v.get(0); // (type error)  compilation-time error

Example from typescript:

function identity<T>(arg: T): T {
   return arg;

Last updated June 22nd, 2020

Edit Generics on GitHub