Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Generic Programming

Generic Programming

Ratings: (0)|Views: 476 |Likes:
Published by sgganesh
see www.joyofprogramming.com
see www.joyofprogramming.com

More info:

Published by: sgganesh on Jun 02, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





An Introduction to Generic Programming 
Advances in programming languages are influenced by various factors. One of the major drivingforces in programming language design, particularly for imperative languages like Fortran and C, was
. With software engineering becoming more mature, computers becoming increasinglypowerful, and software becoming more complex, efficiency has ceased to play a central role inlanguage design. There are other important design criterion like better abstraction and reusability thatare becoming more influential.One of the most important influencing factors in language design is the desire to
more detailsto take the language farther from the machine details. Assembly languages abstracted the achineoperations and control flow with operation codes and labels. Imperative languages abstracted themachine level details with more structured control flow and data-types. Various programmingparadigms evolved later providing different ways of abstraction. For example, object orientedlanguages abstracted data and provided higher-level modeling of relationship between the ADTs(Abstract Data Types). Generic programming tends to abstract the type details and helps us movetowards still higher levels of abstraction where the whole program is viewed as various componentsglued together.Another significant factor influencing the design and evolution of contemporary programminglanguages is to provide better facilities for writing
reusable code
. One of the most popular ways ofproviding reusable code so far is the support for creating
, with languages themselvesproviding significant portion of the core facilities common for usual programming tasks. Objectoriented programming languages provided support for creating another form of reusable code:
. Polymorphism and inheritance are the features providing a solid foundation for creatingsuch reusable code. However, frameworks suffer from significant limitations, which restricts itswidespread use (which is not discussed here). Component programming aspires to provide highlyreusable components that can be just plugged together to make them work.The trend in programming language design is clear:
languages are slowly evolving towards providingbetter facilities for abstraction and reusability
. Though generic programming facilities are present in manyimportant languages, it has started becoming sophisticated and gaining increasing importance onlyrecently. Though it has been few decades since generic programming facilities were introduced inexperimental basis in languages, it is only in past few years it has started becoming mature andsophisticated.The reason for much of the recent popularity and attention towards generic programming is due toC++ in which significant support for generic programming is provided through templates, andgeneric programming has become an important programming paradigm supported by C++. Most ofthe C++ libraries are now almost entirely written using templates, signifying the importance of therole that template feature plays in C++.
 What is Generic Programming?
According to [Czarnecki and Eisenecker, 2000],
Generic programming is a subdiscipline of computer science that deals with finding abstractrepresentations of efficient algorithms, data structures, and other software concepts, and with their systematic organization… Generic programming focuses on representing families of domain
.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
 generic programming
in this context.
Component programming
is often used interchangeably in the context of the ability to create pluggable,reusable components. This article doesn’t use the term
component programming
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
 generic units
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
. A
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
. A
container might be viewed as a specialization of
type substituted for parametric type
for both
. 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
 parametric polymorphism
and in dynamically typed languages it is through
dynamic typing
In statically typed languages, instead of writing the same code for different types, it is possible to
the type information and generate code for different types as needed. In this way, it ispossible to write programming components that are 'generic' enough to work on different types.Statically typed languages such as ML, Ada, C++ and Java support this parameteric polymorphism,with C++ being the language providing most comprehensive support for generic programmingthrough
. Languages such as Java provide limited support for parametric polymorphism,mainly to support
in using heterogeneous containers.// C++ example:// addition as a template function.template <typename T>T plus (T arg1, T arg2) {
return (arg1 + arg2);}// sample usageint i = plus(10, 20);float fval = 20.0f;float f = plus(10.0f, fval);
In type parameterization model, the type information associated with code or object is abstracted as acode template. This code template used to generate ("instantiate") type specific code as and whenrequired. In this way, the type specific code can potentially be generated for an infinite set of types. Inother words, this approach parameterizes the type information of the code or object, and hencereferred to as type parameterization. Type parameterization is also referred to as parametricpolymorphism or generics.In dynamically typed languages, it is natural to write generic components, since the methods areinvoked based on the dynamic type of the object. Components can be written independent of the type:if the receiver object understands the message passed, it will respond to the message accordingly(otherwise, it might fail. But dynamically typed languages provide different means to handle thescenario where the
object does not understand the
; for example, in Smalltalk, it resultsin
method (and Smalltalk also provides the facility to
that message to someother message).An important observation in these two approaches is that they are complementary in nature.Parametric polymorphism is possible only when the language follows static typing, and it is notpossible to apply it for dynamically typed languages (since the type checking is postponed to runtimeinstead of performing at compile-time). For example, it is not possible to introduce parametricpolymorphism in Smalltalk, which is a dynamically typed language (in other words, parameterizingclasses based on type doesn’t hold any meaning in Smalltalk). For the same reason, a languagesupporting parametric polymorphism cannot have dynamic typing. For example, it is not possible tohave dynamic typing supported in C++, which is a statically typed language supporting parametricpolymorphism (in other words, to parameterize classes based on type, type checking has to be done atcompile time).There are many object-oriented languages that have a common universal base class (typically namedas the
base class) is used for representing the operations that are universally applicable to all

Activity (7)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
mdiazw1 liked this
ronnien liked this
Edwin Raja liked this
mkmanojdevil liked this
vijaybpujari liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->