Generic programming
Such software entities are known as generics in Ada, C#, Delphi, Eiffel, F#, Java, Nim, Python, Go, Rust, Swift, TypeScript, and Visual Basic (.NET).The term generic programming was originally coined by David Musser and Alexander Stepanov[4] in a more specific sense than the above, to describe a programming paradigm in which fundamental requirements on data types are abstracted from across concrete examples of algorithms and data structures and formalized as concepts, with generic functions implemented in terms of these concepts, typically using language genericity mechanisms as described above.For example, given N sequence data structures, e.g. singly linked list, vector etc., and M algorithms to operate on them, e.g. find, sort etc., a direct approach would implement each algorithm specifically for each data structure, giving N × M combinations to implement.Its goal is the incremental construction of systematic catalogs of useful, efficient and abstract algorithms and data structures.It has few words that expose the compiler behaviour and therefore naturally offers genericity capacities that, however, are not referred to as such in most Forth texts.Similarly, dynamically typed languages, especially interpreted ones, usually offer genericity by default as both passing values to functions and value assignment are type-indifferent and such behavior is often used for abstraction or code terseness, however this is not typically labeled genericity as it's a direct consequence of the dynamic typing system employed by the language.Some extensible programming languages try to unify built-in and user defined generic types.For example, in C++, this duplication of code can be circumvented by defining a class template: Above, T is a placeholder for whatever type is specified when the list is created.These "containers-of-type-T", commonly called templates, allow a class to be reused with different datatypes as long as certain contracts such as subtypes and signature are kept.Templates can also be used for type-independent functions as in the Swap example below: The C++ template construct used above is widely cited[citation needed] as the genericity construct that popularized the notion among programmers and language designers and supports many generic programming idioms.C# 2.0, Oxygene 1.5 (formerly Chrome) and Visual Basic (.NET) 2005 have constructs that exploit the support for generics present in Microsoft .NET Framework since version 2.0.When instantiating the unit, the programmer must pass an actual array type that satisfies these constraints.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.Likewise, other templates that rely on < cannot be applied to complex data unless a comparison (in the form of a functor or function) is provided.This allows alternative implementations to be provided based on certain characteristics of the parameterized type that is being instantiated.Class templates can also be fully specialized, which means that an alternate implementation can be provided when all of the parameterizing types are known.Templates avoid some of the common errors found in code that makes heavy use of function-like macros, such as evaluating parameters with side effects twice.There are four primary drawbacks to the use of templates: supported features, compiler support, poor error messages (usually with pre C++20 substitution failure is not an error (SFINAE)), and code bloat: So, can derivation be used to reduce the problem of code replicated because templates are used?Conventionally, D combines the above features to provide compile-time polymorphism using trait-based generic programming.The import expression and compile-time function execution also allow efficiently implementing domain-specific languages.For example, given a function that takes a string containing an HTML template and returns equivalent D source code, it is possible to use it in the following way: Generic classes have been a part of Eiffel since the original method and language design.This ensures a compile time error, if the method is called if the type does not support comparison.In addition, the method would need to access the array items as objects instead, and would require casting to compare two elements.Free Pascal implemented generics in 2006 in version 2.2.0, before Delphi and with different syntax and semantics.Delphi and Free Pascal example: The type class mechanism of Haskell supports generic programming.For example, the following declaration of a type of binary trees states that it is to be an instance of the classes Eq and 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.Ralf Hinze (2004) has shown that a similar effect can be achieved for user-defined type classes by certain programming techniques.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.Both Standard ML and OCaml provide functors, which are similar to class templates and to Ada's generic packages.