Generics
In Programming, the generics of function rests on its independence with respect to the type, and possibly of the number, its arguments. It is an important concept for a High-level language because it makes it possible to increase the level of abstraction of the language. Several mechanisms aiming at allowing the writing of generic functions were thus implemented by the various computer programming languages.
Polymorphism
In a language with dynamic Typing, one uses the polymorphism: the parameters passed in arguments with the function must conform to a given interface (a defining protocol which methods are supported by this argument). This mechanism is supported by all the directed languages objects, including those which one generally qualifies as being with static typing (virtual Java, or methods of the C++ for example). Indeed, even if the language brings tools to check the coherence of the interfaces to compilation, the determination of the real type of the argument (i.e. the resolution of the address of the code implementing the methods of the interface) is actually carried out with the execution.
Generic programming
Principle
An advantage of the generic programming consists in abstracting a unit from coherent concepts to build algorithms with the top independently of their implementation. Thus the function of sorting of the STL needs in entry for random itérateurs on a collection for objects and a function for comparison on these objects; that thus makes this function usable in very many contexts.
This mechanism allows purely static generic programming since the whole of the resolutions of the types is made at the compile time.
For example , if one wants to create a generic list, a declaration (here by using the templates C++) would be:
List
Languages of implementation
The generic programming is supported by certain languages with static Typage in which it is possible to use generic types or “templates”.
The very first implementation was in Ada 83, well before the templates of C++ which popularized the generic programming, while making it possible to write code without considering the Types explicitly data with which the code will be used. This is intensively used in the Standard Template Library (STL).
Others Computer programming languages like Java since version 1.5, Eiffel support the generic programming. Objective Caml also allows rather subtle and very powerful generic programming thanks to the system of parameterized modules which he proposes. VB.NET and C# starts to do it with .NET 2.0.
Optional arguments
Certain languages also make it possible to make arguments optional in their defining a default value. Languages like Python or Java (since version 1.5) or the D in addition make it possible to define functions which accept an arbitrary number of parameters. The Langage C also proposes a mechanism of Fonction variadic intended for the implementation of the variable functions to number of arguments.
Overload and multi-methods
In certain languages, it is possible to use the Surcharge of the operators or the functions. This technique consists in giving the same name to several semantically equivalent functions but acting on parameters of the different type. When it meets this name of function, the compiler chooses the implementation most adapted while being based on the number and the type of the arguments. Languages as CLOSED no longer extends this principle while choosing among the homonymous functions to compilation but to the execution. This mechanism is called Multi-method and is often presented in addition like an extension of polymorphism.
Other techniques
It is also possible to fulfill generic functions in languages not objects. Thus the tool used to develop in C generic functions is the pointer of function. For example the function qsort of the standard library accepts in argument a pointer on a function charged to compare the elements to be sorted.
Zh-yue: 通用編程
| Random links: | Poco Nemo | Youri Bandajevsky | Felix Rohatyn | IC 4444 | Pitous | Matthias Göring | Banlieue_noire_de_Spencer,_Minnesota |