Content deleted Content added
added to distinguish hatnote |
Tags: Mobile edit Mobile web edit |
||
(14 intermediate revisions by 6 users not shown) | |||
Line 70:
==Programming language support for genericity==
Genericity facilities have existed in high-level languages since at least the 1970s in languages such as [[ML (programming language)|ML]], [[CLU (programming language)|CLU]] and [[Ada (programming language)|Ada]], and were subsequently adopted by many [[object-based]] and [[Object-oriented programming|object-oriented]] languages, including [[BETA (programming language)|BETA]], [[C++]], [[D (programming language)|D]], [[Eiffel (programming language)|Eiffel]], [[Java (programming language)|Java]], and [[Digital Equipment Corporation|DEC]]'s now defunct [[Trellis-Owl]].
Genericity is implemented and supported differently in various programming languages; the term "generic" has also been used differently in various programming contexts. For example, in [[Forth (programming language)|Forth]] the [[compiler]] can execute code while compiling and one can create new ''compiler keywords'' and new implementations for those words on the fly. 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.{{citation needed|date=August 2015}} The term has been used in [[functional programming]], specifically in [[Haskell]]-like languages, which use a [[structural type system]] where types are always parametric and the actual code on those types is generic. These uses still serve a similar purpose of code-saving and rendering an abstraction.
Line 80:
===In object-oriented languages===
When creating container classes in statically typed languages, it is inconvenient to write specific implementations for each datatype contained, especially if the code for each datatype is virtually identical. For example, in C++, this duplication of code can be circumvented by defining a class template:
<syntaxhighlight lang="Cpp">
class Animal { ... };
class
template <typename T>
class MyList {
// Class contents.
};
</syntaxhighlight>
Above, <code>T</code> is a placeholder for whatever type is specified when the list is created. These "containers-of-type-T", commonly called [[template (programming)|templates]], allow a class to be reused with different datatypes as long as certain contracts such as [[Subtyping|subtype]]s and [[Type signature|signature]] are kept. This genericity mechanism should not be confused with ''[[polymorphism (computer science)|inclusion polymorphism]]'', which is the [[algorithm]]ic usage of exchangeable sub-classes: for instance, a list of objects of type <code>Moving_Object</code> containing objects of type <code>Animal</code> and <code>Car</code>. Templates can also be used for type-independent functions as in the <code>Swap</code> example below:
<syntaxhighlight lang="Cpp">
// "&" denotes a reference
template <typename T>
void swap(T& a, T& b) noexcept {
b = a;
a = temp;
}
int main(int argc, char* argv[]) {
std::string
swap(world, hello);
std::println("{}{}", world, hello); // Output is "Hello, World!".
}
</syntaxhighlight>
Line 204 ⟶ 213:
=====Technical overview=====
There are many kinds of templates, the most common being function templates and class templates. A ''function template'' is a pattern for creating ordinary functions based upon the parameterizing types supplied when instantiated. For example, the C++ Standard Template Library contains the function template <code>std::max(x, y)</code> that creates functions that return either ''x'' or ''y,'' whichever is larger. <code>max()</code> could be defined like this:
<syntaxhighlight lang="cpp">
template <typename T>
[[nodiscard]]
constexpr T
return x < y ? y : x;
}
</syntaxhighlight>
Line 216 ⟶ 226:
<syntaxhighlight lang="cpp">
std::
</syntaxhighlight>
Line 222 ⟶ 232:
<syntaxhighlight lang="cpp">
[[nodiscard]]
constexpr int max(int x, int y) noexcept {
return x < y ? y : x;
}
</syntaxhighlight>
This works whether the arguments <code>x</code> and <code>y</code> are integers, strings, or any other type for which the expression <code>x
C++ templates are completely [[type safe]] at compile time. As a demonstration, the standard type <code>std::complex</code> does not define the <code>
Another kind of template, a ''class template
[[C++20]] introduces constraining template types using [[Concepts (C++)|concepts]]. Constraining the <code>max()</code> could look something like this:
<syntaxhighlight lang="cpp">
// in typename declaration:
template <std::totally_ordered T>
[[nodiscard]]
constexpr T max(T x, T y) noexcept {
return x < y ? y : x;
}
// in requires clause:
template <typename T>
requires std::totally_ordered<T>
[[nodiscard]]
constexpr T max(T x, T y) noexcept {
return x < y ? y : x;
}
</syntaxhighlight>
=====Template specialization=====
Line 385 ⟶ 415:
static void Main()
{
int[]
MakeAtLeast<int>(
foreach (int i in
{
Console.WriteLine(i); // Print results.
}
Console.ReadKey(true);
}
Line 395 ⟶ 427:
{
for (int i = 0; i < list.Length; i++)
{
if (list[i].CompareTo(lowest) < 0)
{
list[i] = lowest;
}
}
}
}
Line 408 ⟶ 444:
<syntaxhighlight lang="csharp">
using System;
// A generic class
class GenericTest<T>
{
// A static variable - will be created for each type on reflection
static CountedInstances OnePerType = new CountedInstances();
private T _t;
//
public
{
_t = t;
}
}
// a class
{
// Static variable - this will be incremented once per instance
public static int
// Default constructor
public CountedInstances()
{
// Increase counter by one during object instantiation
CountedInstances.Counter++;
}
}
public class GenericExample
{
static void Main(string[] args)
{
// Main code entry point
// At the end of execution, CountedInstances.Counter = 2
GenericTest<int> g1 = new GenericTest<int>(1);
GenericTest<int> g11 = new GenericTest<int>(11);
GenericTest<int> g111 = new GenericTest<int>(111);
GenericTest<double> g2 = new GenericTest<double>(1.0);
}
}
</syntaxhighlight>
Line 667 ⟶ 711:
[[VHDL]], being derived from Ada, also has generic abilities.<ref>https://www.ics.uci.edu/~jmoorkan/vhdlref/generics.html VHDL Reference</ref>
[[C (programming language)|C]]
<syntaxhighlight lang="c">
#define
({ typeof (a) _a =
typeof (b) _b =
_a > _b ? _a : _b; })</syntaxhighlight>
The keyword <code>_Generic</code> is used in C preprocessor macros to automatically match the type of its parameter.
<syntaxhighlight lang="c">
#include <stdio.h>
#define type_of(x) _Generic((x), \
int: "int", \
float: "float", \
double: "double", \
char*: "string", \
default: "unknown")
int main(int argc, char* argv[]) {
printf("%s\n", type_of(42));
printf("%s\n", type_of(3.14f));
printf("%s\n", type_of(2.718));
printf("%s\n", type_of("hello"));
printf("%s\n", type_of((void *)0));
return 0;
}
</syntaxhighlight>
==See also==
Line 719 ⟶ 786:
* [[Free Pascal]]: [https://www.freepascal.org/docs-html/ref/refch8.html Free Pascal Reference guide Chapter 8: Generics], Michaël Van Canneyt, 2007
* Delphi for Win32: [https://sjrd.developpez.com/delphi/tutoriel/generics/ Generics with Delphi 2009 Win32], Sébastien DOERAENE, 2008
* Delphi for .NET: [https://www.felix-colibri.com/papers/oop_components/delphi_generics_tutorial/delphi_generics_tutorial.html Delphi Generics] {{Webarchive|url=https://web.archive.org/web/20230114100915/http://www.felix-colibri.com/papers/oop_components/delphi_generics_tutorial/delphi_generics_tutorial.html |date=14 January 2023 }}, Felix COLIBRI, 2008
;Eiffel
|