![]() |
![]() |
|
![]() |
![]() |
Encyclopedia :
G :
GE :
GEN :
Generic programming |
|
|
Generic programmingIn computer science, generics is a technique that allows one value to take different datatypes (so-called polymorphism) as long as certain contracts such as subtypes and signature are kept. The programming style emphasizing use of this technique is called generic programming. For example, if one wanted to create a list using generics, a possible declaration would be to say List Among object-oriented languages, C++, D, BETA, Eiffel, Ada, and later versions of Java. VB.NET and C# began providing generic facilities with .NET 2.0. It was, however, templates of C++ that popularized the notion of generics. Dynamic typing, such featured in Objective-C, and, if necessary, judicious use of protocols circumvent the need for use of generic programming techniques, since there exists a general type to contain any object. Whilst Java does so also, the casting that needs to be done breaks the discipline of static typing, and generics are one way of achieving the benefits of dynamic typing with the advantages of having static typing.
TemplatesTemplates are of great utility to programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ Standard Template Library (STL) provides many useful functions within a framework of connected templates. As the templates in C++ are very expressive they may be used for things other than generic programming. One such use is called template metaprogramming, which is a way of pre-evaluating some of the code at compile-time rather than run-time. Further discussion here only relates to templates as a method of generic programming. Technical overview There are two kinds of templates. A function template behaves like a function that can accept arguments of many different types. For example, the C++ Standard Template Library contains the function template
template T max(T x, T y) { if (x < y) return y; else return x; } This template can be called just like a function:
cout << max(3, 7); // outputs 7 The compiler determines by examining the arguments that this is a call to This works whether the arguments As a counterexample, the standard type A class template extends the same concept to classes. Class templates are often used to make generic containers. For example, the STL has a linked list container. To make a linked list of integers, one writes Advantages and disadvantages Some uses of templates, such as the #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. First, many compilers historically have very poor support for templates, so the use of templates can make code somewhat less portable. Second, almost all compilers produce confusing, unhelpful error messages when errors are detected in template code. This can make templates difficult to develop. Third, each use of a template may cause the compiler to generate extra code (an instantiation of the template), so the indiscriminate use of templates can lead to code bloat, resulting in excessively large executables. 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. Generic programming features in other languagesTemplates were left out of some C++-based languages, such as Java and C#, largely due to the problems with templates in C++. These languages have adopted other methods of dealing with the same problems. However, C# is currently adopting generic programming features comparable to templates. Java supports generics as of J2SE 1.5.0. Generics In Java D supports template programming as advanced as C++'s, Ada's generics predate templates. Generic programming in HaskellIn Haskell in particular, some language extensions have been developed for generic programming, and the language itself contains some generic aspects included. In the language itself For instance, in a declaration of a user-defined data type (a binary tree with values of some given type data BinTree a = Leaf a | Node (BinTree a) a (Bintree a)
deriving (Eq, Show)
, the keyword So, the Haskell compiler can, in a generic fashion, generate instances of particular functions for any given data type (although some restrictions apply). Other instances that can be generated are PolyP extension PolyP was the first generic programming language extension to Haskell. 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 * → *, 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. Generic HaskellGeneric Haskell is another extension to Haskell, developed at Utrecht University. The extensions it provides are: The resulting type-indexed value can be specialised to any type. As an example, the equality function in Generic Haskell: The Scrap your boilerplate approach is a lightweight generic programming approach for Haskell. The approach is supported in the GHC >= 6.0 implementation of Haskell. Using this approach, the programmer can write generic functions such as traversal schemes (e.g., In all the previous language extensions seen, none could work totally inside the Haskell 98 standard: they all require something extra, either a more sophisticated type system or an additional language construct. The purpose of Generics for the masses is to show that one can, in fact, program generically within Haskell 98 obviating to some extent the need for fancy type systems or separate tools. Haskell’s type classes are at the heart of this approach: they ensure that generic functions can be defined succinctly and, in particular, that they can be used painlessly. |
|
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License. |
|
| © 2008 Chamas Enterprises Inc. |