All Topics  
Generic programming

 

   Email Print
   Bookmark   Link






 

Generic programming



 
 
Generic programming is a style of computer programming
Computer programming

Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language....
 in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters and was pioneered by Ada
Ada (programming language)

Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
 which appeared in 1983. This approach permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication
Duplicate code

Duplicate code is a computer programming term for a sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity....
.






Discussion
Ask a question about 'Generic programming'
Start a new discussion about 'Generic programming'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Generic programming is a style of computer programming
Computer programming

Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language....
 in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters and was pioneered by Ada
Ada (programming language)

Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
 which appeared in 1983. This approach permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication
Duplicate code

Duplicate code is a computer programming term for a sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity....
. Software entities created using generic programming are known as generics in Ada, Eiffel
Eiffel (programming language)

Eiffel is an International Organization for Standardization-standardized, object-oriented programming language designed to enable programmers to efficiently develop extensible, reusable, reliable software....
, Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, C#, Visual Basic .NET
Visual Basic .NET

Visual Basic , formerly called Visual Basic .NET , is an object-oriented programming computer language that can be viewed as an evolution of Microsoft Visual Basic implemented on the .NET Framework....
 and Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
; template
Template (programming)

Templates are a feature of the C++ programming language that allow functions and classes to operate with Generic programming. This allows a function or class to work on many different datatype without being rewritten for each one....
s
in C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
; and parameterized types in the influential 1994 book Design Patterns. The authors of Design Patterns note that this technique, especially when combined with delegation
Delegation (programming)

In object-oriented programming there are two related notions of delegation.* Most commonly, it refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems"....
, is very powerful but that "Dynamic, highly parameterized software is harder to understand than more static software." (Gang of Four 1995:21)

Generic programming refers to features of certain statically typed programming languages that allow some code to effectively circumvent the static typing requirements. For instance in C++, a template is a routine in which some parameters are qualified by a type variable. Since code generation in C++ depends on concrete types, the template is specialized for each combination of argument types that occur in practice. Generic programming is commonly used to implement container
Container (data structure)

In computer science, a container is a Class , a data structure, or an abstract data type whose instances are collections of other objects. They are used to store objects in an organized way following specific access rules....
s such as list
List (computing)

In computer science, a list is an ordered Multiset of entity/items.In the context of object-oriented programming languages, a list is defined as an instance of an abstract data type , formalizing the concept of an order theoryed Collection class of entity....
s and hash table
Hash table

In computer science, a hash table, or a hash map, is a data structure that associates Unique key with value .The primary operation that hash functions support efficiently is a lookup: given a key , find the corresponding value ....
s and functions such as a particular sorting algorithm
Sorting algorithm

In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a List in a certain Total order. The most-used orders are numerical order and lexicographical order....
 for objects specified in terms more general than a concrete type.

Programming language support for generic programming

Generic programming facilities first appeared in the 1970s in languages like CLU
CLU programming language

CLU is a programming language created at Massachusetts Institute of Technology by Barbara Liskov and her students between 1974 and 1975. It was notable for its use of constructors for abstract data types that included the code that operated on them, a key step in the direction of object-oriented programming ....
 and Ada, and were subsequently adopted by many object-based and object-oriented
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 languages, including BETA, C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
, D, Eiffel
Eiffel (programming language)

Eiffel is an International Organization for Standardization-standardized, object-oriented programming language designed to enable programmers to efficiently develop extensible, reusable, reliable software....
, Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, and DEC
Dec

DEC, dec or Dec may refer to:Places* Dec, a village in Serbia* Decatur Airport, Decatur, Illinois * Derwent Entertainment Centre, an entertainment centre in Hobart, Australia...
's now defunct Trellis-Owl language. Implementations of generics in languages such as Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
 and C# are formally based on the notion of parametricity
Parametricity

In the theory of programming languages in computer science, parametricity is a theoretical result that states that functions that have types that are related to each other share certain properties in common....
, due to John C. Reynolds
John C. Reynolds

John C. Reynolds is an United States computer scientist.John Reynolds studied at Purdue University and then earned a PhD in theoretical physics from Harvard University in 1961....
. Software entities created using generic programming are known as generics in Ada, Eiffel, Java, C#, VB .NET and Haskell; templates in C++; and parameterized types in Design Patterns.

Generic programming is implemented and supported differently within each language. The term "generic" has also been used differently in programming contexts. For example, in Forth the compiler can execute code while compiling and one can create new compiler keywords and new implementations for those words on the fly. It has few words that expose the compiler behaviour and therefore naturally offers generic programming capacities which, however, are not referred to as such in most Forth texts. The term has been used in functional programming
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
, specifically in Haskell-like
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
 languages, which use a structural type system
Structural type system

A structural type system is a major class of type system, in which type compatibility and equivalence are determined by the type's structure, and not through explicit declarations....
 where types are always parametric and the actual code on those types is generic. In these uses generic programming still serves the similar purpose of code-saving and the rendering of an abstraction.

Generic programming in object-oriented languages


When creating containers of objects it is possible to write specific implementations for each datatype contained, even if the code is virtually identical except for different datatypes. In C++, this duplication of code can be circumvented by defining a template class: template class List ;

List list_of_animals; List list_of_cars; Above, T is a placeholder for whatever type is specified when the list is created. These "containers-of-type-T", commonly called generics, are a generic programming technique allowing a class to be reused with different datatypes as long as certain contracts such as subtype
Subtype

In computer science, a subtype is a datatype that is generally related to another datatype by some notion of substitutability, meaning that computer programs written to operate on elements of the supertype can also operate on elements of the subtype....
s and signature
Signature (computer science)

The signature of a function is roughly equivalent to its prototype definition in the C .It contains the name of the function as well as its parameters and their type....
 are kept. Generic programming should not to be confused with inclusion polymorphism, which is the algorithm
Algorithm

In mathematics, computing, linguistics and related subjects, an algorithm is a sequence of finite instructions, often used for calculation and data processing....
ic usage of exchangeable sub-classes: for instance, a list of objects of type Moving_Object containing objects of type Animal and Car. Generics can also be used for type-independent functions as in the Swap example below:

template void Swap(T & a, T & b) //"&" passes parameters by reference

string hello = "world!", world = "Hello, "; Swap( world, hello ); cout << hello << world << endl; //Output is "Hello, world!"

The C++ template construct used above is widely cited as the generic programming construct that popularized the notion among programmers and language designers and supports many generic programming idioms. The D programming language also offers fully generic-capable templates based on the C++ precedent but with a simplified syntax. The Java programming language has provided generic programming facilities syntactically based on C++'s since the introduction of J2SE
Java Platform, Standard Edition

Java Platform, Standard Edition or Java SE is a widely used Platform for programming in the Java language. It is the Java Platform used to deploy porting Application software for general use....
 5.0 and implements the generics subset of generic programming.

Although the examples above are a common use of generic programming, and some languages implement only this aspect of it, the concept of generic programming is not limited to "generics" as a programming language construct. In Python, a language with strong, dynamic typing, generic programming is both transparent and also the simplest way to write routines. For example, the preceding example can be translated into Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
 if a box class is created to simulate the C++ notion of call-by-reference. class Box: def __init__(self,ob): self.contents = ob

def swap(box1,box2): box2.contents, box1.contents = box1.contents, box2.contents

C# 2.0, Chrome 1.5 and Visual Basic .NET 2005
Visual Basic .NET

Visual Basic , formerly called Visual Basic .NET , is an object-oriented programming computer language that can be viewed as an evolution of Microsoft Visual Basic implemented on the .NET Framework....
 have constructs that take advantage of the support for generics present in the Microsoft .NET Framework
.NET Framework

The Microsoft .NET Framework is a software framework that is available with several Microsoft Windows operating systems. It includes a large Library of coded solutions to prevent common programming problems and a virtual machine that manages the execution of programs written specifically for the Software framework....
 since version 2.0. The ML
ML programming language

ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM....
 family of programming languages encourage generic programming through parametric polymorphism and generic modules called functors. The type class
Type class

In computer science, a type class is a type system construct that supports Type polymorphism#Ad-hoc polymorphism. This is achieved by adding constraints to type variables in Parametric polymorphism#Parametric polymorphism types....
 mechanism of Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
 supports generic programming.

Dynamic typing, such as is featured in Objective-C
Objective-C

Objective-C is a Reflection , Object-oriented programming programming language which adds Smalltalk-style message passing to C .Today it is used primarily on Mac OS X, iPhone OS, and GNUstep, three environments based on the OpenStep standard, and is the primary language used for the NEXTSTEP, OpenStep#OPENSTEP, and Cocoa application framew...
, and, judicious use of protocols circumvent the need for use of generic programming techniques, since there exists a general type to contain any object. While 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.

Generics in Ada

Ada
Ada (programming language)

Ada is a structured programming, statically typed, Imperative programming, and Object-oriented programming high-level language computer programming programming language, extended from Pascal and other languages....
 has had generics since it was first designed in 1977-1980. The standard library uses generics to provide many services. Ada 2005 adds a comprehensive generic container library to the standard library, which was inspired by C++'s standard template library.

A generic unit is a package or a subprogram that takes one or more generic formal parameters.

A generic formal parameter is a value, a variable, a constant, a type, a subprogram, or even an instance of another, designated, generic unit. For generic formal types, the syntax distinguishes between discrete, floating-point, fixed-point, access (pointer) types, etc. Some formal parameters can have default values.

To instantiate a generic unit, the programmer passes actual parameters for each formal. The generic instance then behaves just like any other unit. It is possible to instantiate generic units at run-time, for example inside a loop.

Ada example

The specification of a generic package:

generic 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; private 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;

Instantiating the generic package:

type Bookmark_Type is new Natural; -- records a location in the text document we are editing

package Bookmark_Stacks is new Stacks (Max_Size => 20, Element_Type => Bookmark_Type); -- Allows the user to jump between recorded locations in a document

Using an instance of a generic package:

type Document_Type is record Contents : Ada.Strings.Unbounded.Unbounded_String; Bookmarks : Bookmark_Stacks.Stack; end record;

procedure Edit (Document_Name : in String) is Document : Document_Type; begin -- Initialise the stack of bookmarks: Bookmark_Stacks.Create (S => Document.Bookmarks, Initial_Size => 10); -- Now, open the file Document_Name and read it in... end Edit;

Advantages and limitations

The language syntax allows precise specification of constraints on generic formal parameters. For example, it is possible to specify that a generic formal type will only accept a modular type as the actual. It is also possible to express constraints between generic formal parameters; for example:

generic type Index_Type is (<>); -- must be a discrete type type Element_Type is private; -- can be any nonlimited type type Array_Type is array (Index_Type range <>) of Element_Type;

In this example, Array_Type is constrained by both Index_Type and Element_Type. When instantiating the unit, the programmer must pass an actual array type that satisfies these constraints.

The disadvantage of this fine-grained control is a complicated syntax, but, because all generic formal parameters are completely defined in the specification, the compiler can instantiate generics without looking at the body of the generic.

Unlike C++, Ada does not allow specialised generic instances, and requires that all generics be instantiated explicitly. These rules have several consequences:

  • the compiler can implement shared generics: the object code for a generic unit can be shared between all instances (unless the programmer requests inlining of subprograms, of course). As further consequences:
    • there is no possibility of code bloat (code bloat is common in C++ and requires special care, as explained below).
    • it is possible to instantiate generics at run time, as well as at compile time, since no new object code is required for a new instance.
    • actual objects corresponding to a generic formal object are always considered to be nonstatic inside the generic; see Generic formal objects in the Wikibook for details and consequences.
  • all instances of a generic being exactly the same, it is easier to review and understand programs written by others; there are no "special cases" to take into account.
  • all instantiations being explicit, there are no hidden instantiations that might make it difficult to understand the program.
  • Ada does not permit "template metaprogramming", because it does not allow specialisations.


Templates in C++
Templates are of great utility to programmers in C++, especially when combined with multiple inheritance
Multiple inheritance

Multiple inheritance refers to a feature of some object-oriented programming programming languages in which a class can inheritance behaviors and features from more than one superclass ....
 and operator overloading
Operator overloading

In computer programming, operator overloading is a specific case of polymorphism in which some or all of operator s like +, =, or have different implementations depending on the types of their arguments....
. The C++ Standard Template Library
Standard Template Library

The Standard Template Library is a Library partially included in the C++ C++ standard library. It provides Container s, iterators, algorithms, and Function objects....
 (STL) provides a framework of connected templates. Templates in C++ may also be used for template metaprogramming
Template metaprogramming

Template metaprogramming is a metaprogramming technique in which Generic programming are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled....
, which is a way of pre-evaluating some of the code at compile-time rather than run-time. Due to Template specialization, C++ Templates are considered turing complete.

Technical overview
There are two kinds of templates: function templates and class templates. A function template is a pattern for creating ordinary functions based upon the parameterizing types supplied when instantiated. For example, the C++ Standard Template Library contains the function template max(x, y) which creates functions that return either x or y, whichever is larger. max could be defined like this:

template T max(T x, T y)



Specializations of this function template, instantiations with specific types, can be called just like an ordinary function:

cout << max(3, 7); // outputs 7

The compiler examines the arguments used to call max and determines that this is a call to max(int, int). It then instantiates a version of the function where the parameterizing type T is int, making the equivalent of the following function:

int max(int x, int y)



This works whether the arguments x and y are integers, strings, or any other type for which the expression x < y is sensible, or more specifically, for any type for which operator< is defined. Common inheritance is not needed for the set of types that can be used, and so it is very similar to duck typing
Duck typing

In computer programming, duck typing is a style of dynamic typing in which an object's current set of Method s and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface....
. A program defining a custom data type can use operator overloading to define the meaning of < for that type, thus allowing its use with the max function template. While this may seem a minor benefit in this isolated example, in the context of a comprehensive library like the STL it allows the programmer to get extensive functionality for a new data type, just by defining a few operators for it. Merely defining < allows a type to be used with the standard sort, stable_sort, and binary_search algorithms or to be put inside data structures such as sets, heaps, and associative arrays.

C++ templates are completely type safe
Type safety

In computer science, type safety is a property of some programming languages that is defined differently by different communities, but most definitions involve the use of a type system to prevent certain erroneous or undesirable program behavior ....
 at compile time. As a demonstration, the standard type complex does not define the < operator, because there is no strict order on complex number
Complex number

In mathematics, the complex numbers are an extension of the real numbers obtained by adjoining an imaginary unit, denoted i, which satisfies:...
s. Therefore max(x, y) will fail with a compile error if x and y are complex values. Likewise, other templates that rely on < cannot be applied to complex data. Unfortunately, compilers historically generate somewhat esoteric, long, and unhelpful error messages for this sort of error. Ensuring that a certain object adheres to a method protocol can alleviate this issue.

The second kind of template, a class template, extends the same concept to classes. A class template specialization is a class. Class templates are often used to make generic containers. For example, the STL has a linked list
Linked list

In computer science, a linked list is one of the fundamental data structures, and can be used to implement other data structures. It consists of a sequence of node s, each containing arbitrary data Field s and one or two reference s pointing to the next and/or previous nodes....
 container. To make a linked list of integers, one writes list<int>. A list of strings is denoted list<string>. A list has a set of standard functions associated with it, which work for any compatible parameterizing types.

Template specialization
A powerful feature of C++'s templates is template specialization. This allows alternative implementations to be provided based on certain characteristics of the parameterized type that is being instantiated. Template specialization has two purposes: to allow certain forms of optimization, and to reduce code bloat.

For example, consider a sort template function. One of the primary activities that such a function does is to swap or exchange the values in two of the container's positions. If the values are large (in terms of the number of bytes it takes to store each of them), then it is often quicker to first build a separate list of pointers to the objects, sort those pointers, and then build the final sorted sequence. If the values are quite small however it is usually fastest to just swap the values in-place as needed. Furthermore if the parameterized type is already of some pointer-type, then there is no need to build a separate pointer array. Template specialization allows the template creator to write different implementations and to specify the characteristics that the parameterized type(s) must have for each implementation to be used.

Unlike function templates, class templates can be partially specialized
Partial template specialization

Partial template specialization is a particular form of class template specialization. Usually used in reference to the C++ programming language, it allows the programmer to specialize only some arguments of a class template, as opposed to explicit specialization, where all the template arguments are provided....
. That means that an alternate version of the class template code can be provided when some of the template parameters are known, while leaving other template parameters generic. This can be used, for example, to create a default implementation (the primary specialization) that assumes that copying a parameterizing type is expensive and then create partial specializations for types that are cheap to copy, thus increasing overall efficiency. Clients of such a class template just use specializations of it without needing to know whether the compiler used the primary specialization or some partial specialization in each case. Class templates can also be fully specialized, which means that an alternate implementation can be provided when all of the parameterizing types are known.

Advantages and disadvantages
Some uses of templates, such as the max function, were previously filled by function-like preprocessor
Preprocessor

In computer science, a preprocessor is a Computer program that processes its input data to produce output that is used as input to another program....
 macros (a legacy of the C programming language
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
). For example, here is a possible max macro:

  1. define max(a,b) ((a) < (b) ? (b) : (a))


Both macros and templates are expanded at compile time. Macros are always expanded inline; templates can also be expanded as inline functions when the compiler deems it appropriate. Thus both function-like macros and function templates have no run-time overhead.

However, templates are generally considered an improvement over macros for these purposes. Templates are type-safe. Templates avoid some of the common errors found in code that makes heavy use of function-like macros. Perhaps most importantly, templates were designed to be applicable to much larger problems than macros.

There are three primary drawbacks to the use of templates: compiler support, poor error messages, and code bloat. Many compilers historically have poor support for templates, thus the use of templates can make code somewhat less portable. Support may also be poor when a C++ compiler is being used with a linker
Linker

In computer science, a linker or link editor is a computer program that takes one ormore object file generated by a compiler and combines them into a single executable program....
 which is not C++-aware, or when attempting to use templates across shared library
Library (computer science)

In computer science, a library is a collection of subroutines or Class used to develop software. Libraries contain code and data that provide services to independent programs....
 boundaries. Most modern compilers however now have fairly robust and standard template support, and the new C++ standard, C++0x
C++0x

C++0x is the planned new Open standard for the C++. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003....
, is expected to further address these issues.

Almost all compilers produce confusing, long, or sometimes unhelpful error messages when errors are detected in code that uses templates. This can make templates difficult to develop.

Finally, the use of templates requires the compiler to generate a separate instance of the templated class or function for every permutation
Permutation

In several fields of mathematics the term permutation is used with different but closely related meanings. They all relate to the notion of mapping the element s of a set to other elements of the same set, i.e., exchanging elements of a set....
 of type parameters used with it. (This is necessary because types in C++ are not all the same size, and the sizes of data fields are important to how classes work.) So the indiscriminate use of templates can lead to code bloat
Code bloat

Code bloat is the production of code that is perceived as unnecessarily long, slow, or otherwise wasteful of resources.Code bloat can also be caused by inadequacies in the language in which the code is written, or inadequacies in the compiler used to compile the language....
, resulting in excessively large executables. However, judicious use of template specialization can dramatically reduce such code bloat in some cases. The extra instantiations generated by templates can also cause debuggers to have difficulty working gracefully with templates. For example, setting a debug breakpoint within a template from a source file may either miss setting the breakpoint in the actual instantiation desired or may set a breakpoint in every place the template is instantiated.

Also, because the compiler needs to perform macro-like expansions of templates and generate different instances of them at compile time, the implementation source code for the templated class or function must be available (e.g. included in a header) to the code using it. Templated classes or functions, including much of the Standard Template Library (STL), cannot be compiled. (This is in contrast to non-templated code, which may be compiled to binary, providing only a declarations header file for code using it.) This may be a disadvantage by exposing the implementing code, which removes some abstractions, and could restrict its use in closed-source projects.

Templates in D
The D programming language supports templates that are evolved from those in C++. Most C++ template idioms will carry over to D without alteration, but D adds functionality that streamlines some common cases.

The most obvious differences are syntax changes. D uses parentheses instead of angle-brackets < > in a template definition. It also uses the ! construct (that is, parentheses preceded by an exclamation point) instead of angle-brackets in template instantiation. Therefore, a!(b) in D is the equivalent of a<b> in C++. These changes were made in the interest of making templates easier to parse, as using angle-brackets can lead to ambiguous syntax.

Static-if
D provides a static if construct that checks conditions at compile-time. This is vaguely analogous to C++'s #if and #endif preprocessor macros
Preprocessor

In computer science, a preprocessor is a Computer program that processes its input data to produce output that is used as input to another program....
s. The major difference is that static if has access to all compile-time values, including template arguments. Therefore, many situations which require template specialization in C++ may be written inline in D. Recursive templates in D can look almost identical to their runtime equivalents. This is shown in the classic compile-time factorial template.

template factorial(uint n)

Alias parameters
Templates in D may also accept alias parameters. Alias parameters are similar to C++'s typedef but can also substitute templates parameters. This is a superset of the functionality of template arguments in C++, and will be added in the forthcoming C++0x
C++0x

C++0x is the planned new Open standard for the C++. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003....
 specification. Alias parameters may be templates, functions, types, or any other compile-time symbol. This allows a coder to, for example, "inject" a function into the middle of a template function.

template wrapper(alias Fn)

This sort of template might be useful when interfacing D code with a C API. If a hypothetical C API wants a function pointer, you might use the template like this:

void foo

some_c_function(&wrapper!(foo));

Generics in Java

Support for the generics, or "containers-of-type-T", subset of generic programming were added to the Java programming language
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
 in 2004 as part of J2SE 5.0. In Java, generics are checked at compile time for type correctness. The generic type information is then removed via a process called type erasure, and is unavailable at runtime. For example, a List<String> is converted to the raw type List. The compiler inserts type casts
Type conversion

In computer science, type conversion or typecasting refers to changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies....
 to convert the elements to the String type when they are retrieved from the list.

Generic programming in C# and .NET

Generics in C# (and other .NET languages) were added as part of .NET Framework 2.0
.NET Framework

The Microsoft .NET Framework is a software framework that is available with several Microsoft Windows operating systems. It includes a large Library of coded solutions to prevent common programming problems and a virtual machine that manages the execution of programs written specifically for the Software framework....
 in November 2005. Although similar to generics in Java, .NET generics do not apply type erasure, but implement generics as a first class object in the runtime using reification
Reification (computer science)

Reification is a process through which a computable/addressable object - a resource - is created in a system, as a proxy for a non computable/addressable object....
. This design choice provides additional functionality, such as allowing reflection
Reflection (computer science)

In computer science, reflection is the process by which a computer program can observe and modify its own structure and behaviour. The programming paradigm driven by reflection is called reflective programming....
 with preservation of generic types, as well as alleviating some of the limitations of erasure (such as being unable to create generic arrays). This also means that there is no performance hit from runtime casts
Type conversion

In computer science, type conversion or typecasting refers to changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies....
 and normally expensive boxing conversions. When primitive and value types are used as generic arguments, they get specialized implementations, allowing for efficient generic collections and methods. As in C++ and Java, nested generic types such as Dictionary<string, List<int>> are valid types.

C# (and .NET in general) allows six varieties of generic type constraints using the where keyword including restricting generic types to be value types, to be classes, to have constructors, and to inherit from interfaces. Below is an example with an interface constraint:

class Sample

Since all arrays implement the IList interface (commonly associated with list
List (computing)

In computer science, a list is an ordered Multiset of entity/items.In the context of object-oriented programming languages, a list is defined as an instance of an abstract data type , formalizing the concept of an order theoryed Collection class of entity....
 classes), the MakeAtLeast method allows operation on arrays, with elements of type T. The method's type constraint indicates that the method is applicable to any type T that implements the generic IComparable<T> interface. This ensures a compile time
Compile time

In computer science, compile time refers to either the operations performed by a compiler , programming language requirements that must be met by source code for it to be successfully compiled , or properties of the program that can be reasoned about at compile time....
 error if the method is called with a list of another type. The interface provides the generic method CompareTo(T).

The above method could also be written without generic types, simply using the non-generic IList type. However, this would make the method less type safe, as it would allow the method to be applied to a list of incomparable items, resulting in a run time
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
 error. The method would need to access the list items as objects instead, and would require casting
Type conversion

In computer science, type conversion or typecasting refers to changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies....
 to compare two elements. (For value types like types such as int this requires a boxing conversion, although this can be worked around using the Comparer<T> class, as is done in the standard collection classes.)

Generic programming in Delphi
Generic support was added to Delphi language in October 2008. The first generic support to Generics was Delphi 2007 .NET in 2006, but it was only for .NET platform, and real support for Generics in Delphi was added in Delphi 2009.

Generic programming in Functional languages


Generic programming in Haskell
Six of the predefined type classes in Haskell (including Eq, the types that can be compared for equality, and Show, the types whose values can be rendered as strings) have the special property of supporting derived instances. This means that a programmer defining a new type can state that this type is to be an instance of one of these special type classes, without providing implementations of the class methods as is usually necessary when declaring class instances. All the necessary methods will be "derived" -- that is, constructed automatically -- based on the structure of the type. For instance, the following declaration of a type of binary tree
Binary tree

In computer science, a binary tree is a Tree in which each node has at most two child node. Typically the child nodes are called left and right....
s states that it is to be an instance of the classes Eq and Show:

data BinTree a = Leaf a | Node (BinTree a) a (BinTree a) deriving (Eq, Show)

This results in an equality function (

) and a string representation function (show) being automatically defined for any type of the form BinTree T provided that T itself supports those operations.

The support for derived instances of Eq and Show makes their methods

and show generic in a qualitatively different way from parametrically polymorphic functions: these "functions" (more accurately, type-indexed families of functions) can be applied to values of various types, and although they behave differently for every argument type, little work is needed to add support for a new type. Ralf Hinze (2004) has shown that a similar effect can be achieved for user-defined type classes by certain programming techniques. Other researchers have proposed approaches to this and other kinds of genericity in the context of Haskell and extensions to Haskell (discussed below).

PolyP
PolyP was the first generic programming language extension to Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
. In PolyP, generic functions are called polytypic. The language introduces a special construct in which such polytypic functions can be defined via structural induction over the structure of the pattern functor of a regular datatype. Regular datatypes in PolyP are a subset of Haskell datatypes. A regular datatype t must be of kind
Kind (type theory)

In computer science and type theory, a kind is a "type of a type". Kinds appear, either explicitly or implicitly, in certain languages with complex type systems, such as Haskell and Scala ....
 * ? *, and if a is the formal type argument in the definition, then all recursive calls to t must have the form t a. These restrictions rule out higher-kinded datatypes as well as nested datatypes, where the recursive calls are of a different form. The flatten function in PolyP is here provided as an example: flatten Regular d => d a -> [a] flatten = cata fl

polytypic fl f a [a] -> [a] case f of g+h -> either fl fl g*h -> \(x,y) -> fl x ++ fl y -> \x -> [] Par -> \x -> [x] Rec -> \x -> x d@g -> concat . flatten . pmap fl Con t -> \x -> []

cata Regular d => (FunctorOf d a b -> b) -> d a -> b

Generic Haskell
Generic Haskell is another extension to Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
, developed at Utrecht University
Utrecht University

Utrecht University is a university in Utrecht , The Netherlands. It is one of the oldest universities in the Netherlands and one of the largest in Europe....
 in The Netherlands. The extensions it provides are:
  • Type-indexed values are defined as a value indexed over the various Haskell type constructors (unit, primitive types, sums, products, and user-defined type constructors). In addition, we can also specify the behaviour of a type-indexed values for a specific constructor using constructor cases, and reuse one generic definition in another using default cases.
The resulting type-indexed value can be specialised to any type.
  • Kind-indexed types are types indexed over kinds, defined by giving a case for both * and k ? k. Instances are obtained by applying the kind-indexed type to a kind.
  • Generic definitions can be used by applying them to a type or kind. This is called generic application. The result is a type or value, depending on which sort of generic definition is applied.
  • Generic abstraction enables generic definitions be defined by abstracting a type parameter (of a given kind).
  • Type-indexed types are types which are indexed over the type constructors. These can be used to give types to more involved generic values. The resulting type-indexed types can be specialised to any type.


As an example, the equality function in Generic Haskell:

type Eq t1 t2 = t1 -> t2 -> Bool type Eq t1 t2 = forall u1 u2. Eq u1 u2 -> Eq (t1 u1) (t2 u2)

eq eq
eq
eq
eq
eq
eq
) > eq
) > eq
) >

The "Scrap your boilerplate" approach
The Scrap your boilerplate
Boilerplate (text)

Boilerplate is any text that is or can be reused in new contexts or applications without being changed much from the original. Many computer programmers often use the term #Boilerplate code....
approach is a lightweight generic programming approach for Haskell (Lämmel and Peyton Jones, 2003). A for this approach explains which components of it are currently implemented in GHC
Glasgow Haskell Compiler

The Glorious Glasgow Haskell Compilation System, more commonly known as the Glasgow Haskell Compiler or GHC, is an open source Machine language compiler for the functional programming Computer programming Programming language Haskell ....
.
Uniplate is an even simpler package with a similar basic approach.

Clean

Clean offers generic programming based PolyP and the generic Haskell as supported by the GHC>=6.0. It parametrizes by kind as those but offers overloading.

Generic programming features in other languages

Standard ML
Standard ML

Standard ML is a general-purpose, Module , functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of automated theorem proving....
 and OCaml provide functors, which are similar to class templates and to Ada's generic packages. Scheme syntactic abstractions also have connection to generic programming -- these are in fact a superset of templating a la C++.

See also

  • Partial evaluation
    Partial evaluation

    In computing, partial evaluation is a technique for optimization by specialization.A computer program, prog, is seen as a mapping of input data into output data:...
  • Concept (generic programming)
    Concept (generic programming)

    In generic programming, a concept is a description of supported operations on a type, including syntax and semantics. In this way, concepts are related to abstract base classes but concepts do not require a subtype relationship....
  • Type polymorphism
    Type polymorphism

    In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface....


External links and references


General

  • Bertrand Meyer. "." In OOPSLA (First ACM Conference on Object-Oriented Programming Systems, Languages and Applications), Portland (Oregon), September 29–October 2, 1986, pages 391–405.
  • Gabriel Dos Reis and Jaakko Järvi, , .
  • Alexander A. Stepanov, (creator of the STL
    Standard Template Library

    The Standard Template Library is a Library partially included in the C++ C++ standard library. It provides Container s, iterators, algorithms, and Function objects....
    )


C++/D

  • Walter Bright, .
  • David Vandevoorde, C++ Templates: The Complete Guide, 2003 Addison-Wesley. ISBN 0-201-73484-2


C#/.NET

  • Jason Clark, "," September 2003, MSDN Magazine, Microsoft.
  • Jason Clark, "," October 2003, MSDN Magazine, Microsoft.
  • M. Aamir Maniar, . An open source generics library for C#.


Delphi

  • Nick Hodges, "," October 2008, CodeGear Developer Network, CodeGear.
  • Craig Stuntz, "," October 2008
  • Dr. Bob, ""


Haskell/functional

  • Dæv Clarke, Johan Jeuring and Andres Löh,
  • Ralf Hinze, "," In Proceedings of the ACM
    Association for Computing Machinery

    The Association for Computing Machinery, or ACM, was founded in 1947 as the world's first scientific and educational computing society. Its membership was approximately 83,000 as of 2007....
     SIGPLAN
    SIGPLAN

    SIGPLAN is the Association for Computing Machinery's Special Interest Group on programming languages....
     International Conference on Functional Programming
    International Conference on Functional Programming

    The International Conference on Functional Programming is an annual academic conference in the field of computer science sponsored by the Association for Computing Machinery SIGPLAN, in association with International Federation for Information Processing Working Group 2.8 ....
     (ICFP), 2004.
  • Simon Peyton Jones
    Simon Peyton Jones

    Simon Peyton Jones is a British computer scientist who researches the implementation and application software of functional programming languages, particularly lazy evaluation....
    , editor,
    , Revised 2002.
  • Ralf Lämmel and Simon Peyton Jones
    Simon Peyton Jones

    Simon Peyton Jones is a British computer scientist who researches the implementation and application software of functional programming languages, particularly lazy evaluation....
    , "Scrap Your Boilerplate: A Practical Design Pattern for Generic Programming," In
    Proceedings of the ACM
    Association for Computing Machinery

    The Association for Computing Machinery, or ACM, was founded in 1947 as the world's first scientific and educational computing society. Its membership was approximately 83,000 as of 2007....
     SIGPLAN
    SIGPLAN

    SIGPLAN is the Association for Computing Machinery's Special Interest Group on programming languages....
     International Workshop on Types in Language Design and Implementation (TLDI'03), 2003. (Also see the website )
  • Andres Löh, , Ph.D. thesis, 2004 Utrecht University
    Utrecht University

    Utrecht University is a university in Utrecht , The Netherlands. It is one of the oldest universities in the Netherlands and one of the largest in Europe....
    . ISBN 90-393-3765-9


Java

  • Gilad Bracha, , 2004.
  • Maurice Naftalin and Philip Wadler, Java Generics and Collections, 2006, O'Reilly Media, Inc. ISBN 0-596-52775-6
  • Peter Sestoft, Java Precisely, Second Edition, 2005 MIT Press. ISBN 0-262-69325-9
, 2004 Sun Microsystems, Inc.
  • Angelika Langer,


Object Pascal

  • Free Pascal
    Free Pascal

    Free Pascal is a free software, Portability , open source, Pascal programming language and Object Pascal compiler. The 32/64-bit multi-CPU architecture and cross-platform compiler implements the Borland Pascal programming language dialects as well as some MacPascal constructs, and is available for...
    : , Michaël Van Canneyt, 2007
  • Delphi for Win32: , Sébastien DOERAENE, 2008
  • Delphi for .NET: , Felix COLIBRI, 2008