.There is no universally accepted definition for generic programming. We refer to genericprogramming as
the ability to write reusable, independent programming units that can be plugged together bywriting glue code
. This article uses the term
in this context.
is often used interchangeably in the context of the ability to create pluggable,reusable components. This article doesn’t use the term
since this term iswidely used to refer to the technologies like COM, CORBA and EJB, whose objectives are similar tothat of generic programming, but are essentially quite different in spirit. For example, when you usethe term
in the context of any of these technologies, it is quite different from what youintend to communicate when you refer to
in the context of parametric polymorphism.
Theoretical BasicsStatic and Dynamic Typing
Based on the type systems, languages can be classified in to two main categories: statically anddynamically typed languages. Statically typed languages do type checking at compile-time to preventtype errors. Dynamically typed languages postpone the type checking check to runtime. The mainadvantage in following static typing is that the type errors can be caught at compile-time itself, thus,earlier in the programming lifecycle, and hence easier to fix. Most of the high-level languages arestatically typed languages and are of widespread use because of this property (for example, C++ and Java).On the other hand, dynamically typed languages offer flexibility, particularly when the types of thedata are not known in advance. Hence dynamically typed languages advocate Rapid ApplicationDevelopment (RAD); Smalltalk and Python are examples of languages following dynamic typing.
Generics and Inheritance
Those who are acquainted with object-oriented languages might have a misconception thatinheritance is a substitute for generic units. For example, in a language supporting a universal baseclass Object, and inheritance, a container for storing
pairs can be provided as a
container with two handles to
container, which maps two
might be viewedas a derived class of
. With a language supporting generic types, a
might be viewed as acontainer type parameterized by type
container might be viewed as a specialization of
type substituted for parametric type
. Such common solutionsto similar problems might mislead a novice programmer that these two features can be usedinterchangeably.While it is true that generics and inheritance can be used interchangeably in few cases, in fact thefeatures are complementary in nature. “
Parameterized types give us a third way (in addition to classinheritance and object composition) to compose behavior in object-oriented systems
, notes [Gamma et al,1995].Also, it is possible to mix genericity and inheritance, which is a powerful concept. This idea is clearlyarticulated in a classic paper [Meyers, 1986].
Two Models for Generic Programming
There are two popular and well-known means for creating generic components. In statically typedlanguages, it is through
and in dynamically typed languages it is through