GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Main page
  • Contents
  • Current events
  • Random article
  • About Wikipedia
  • Contact us
  • Help
  • Learn to edit
  • Community portal
  • Recent changes
  • Upload file
  • Special pages
  • Search
  • Donate

This view

  • Original page
  • New address

GeezerVison is showing en.wikipedia.org — Type variable - Wikipedia

In type theory and programming languages, a type variable is a mathematical variable ranging over types. Even in programming languages that allow mutable variables, a type variable remains an abstraction, in the sense that it does not correspond to some memory locations.

Programming languages that support parametric polymorphism make use of universally quantified type variables. Languages that support existential types make use of existentially quantified type variables. For example, the following OCaml code defines a polymorphic identity function that has a universally quantified type, which is printed by the interpreter on the second line:

# let id x = x;;
val id : 'a -> 'a = <fun>

In mathematical notation, the type of the function id is ∀a.a→a, where a is a type variable.

Generic programming

In generic programming, a type parameter or template parameter is used to denote the type(s) that may be consumed. These type parameters are placeholders for types to be specified later, allowing for code to work with different types while remaining type-safe. Type parameters are typically written as a single capital letter.

interface List<E> {
    void add(E x);
    Iterator<E> iterator();
}

interface Map<K, V> {
    V put(K key, V value);
    V get(Object key);
}

In C++, template parameters must be declared with the typename (or class) keyword.

In some languages, these can be specified to have default type parameters. For example, in C++:

template <typename T = int>
class Box {
private:
    T value;
public:
    explicit Box(T value):
        value{value} {}

    // ...
};

Box<double> b1(13.5);
Box<> b2(5); // This is Box<int>

Some languages additionally offer a form of constraining these type parameters, called type classes. C++ offers concepts and requires clauses,[1] C#, Kotlin, and Rust offer where clauses,[2][3] while Java has extends and super qualifiers. Go interfaces also constrain a type such that it must implement certain methods.[4]

using System;

public class MyGenericClass<T, U> 
    where T : IComparable<T>, allows ref struct
    where U : class, notnull, new()
{
    // ...
}

Additionally, Java offers wildcard type parameters to allow for variances of any parametrisations of a generic type. These are denoted ?, for example List<?>.[5] This exists in Kotlin as well, but denoted with * instead. In Rust, a generic type parameter can be left unspecified for the compiler to interpret using _, called an "inferred type" (for example Vec<_>).[6]

In languages with dynamic typing, although type annotations may not be a core part of the language, some features may be provided to emulate them. For example, in Python, type variables can still be represented and specified using the class typing.TypeVar.[7]

from typing import TypeVar

T: TypeVar = TypeVar('T')

def first_item(items: list[T]) -> T:
    return items[0]

In C++ and Rust and others with intricate template metaprogramming, template parameters may be non-types as well.

using std::array;

template <typename T, size_t M, size_t N>
class Matrix {
private:
    array<array<T, N>, M> data;
public:
    Matrix() = default;

    template <typename Self>
    constexpr auto&& operator[](this Self&& self, size_t row, size_t col) {
        return std::forward<Self>(self).data[row][col];
    }   
};

See also

  • Type class
  • Generic programming
  • Template (C++)
  • Generics in Java
  • Template metaprogramming
  • System F

References

  1. ↑ cppreference.com (6 June 2026). "Constraints and concepts". cppreference.com. cppreference.com.
  2. ↑ "where (generic type constraint)". learn.microsoft.com. Microsoft Learn. 30 July 2024.
  3. ↑ "Generics: in, out, where Kotlin". kotlinlang.org. JetBrains s.r.o. Retrieved 15 October 2025.
  4. ↑ The Go Authors. "A tour of Go - Interfaces". go.dev. The Go Authors. Retrieved 5 May 2026.
  5. ↑ "Chapter 4. Types, Values, and Variables". docs.oracle.com. Retrieved 2020-11-03.
  6. ↑ The Rust Devs (16 June 2026). "Inferred type". doc.rust-lang.org. The Rust Reference.
  7. ↑ Python Software Foundation (10 May 2026). "typing — Support for type hints". docs.python.org. Python Software Foundation.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Type_variable&oldid=1371366989"
Categories:
  • Generic programming
  • Type theory
  • Functional programming
  • Dependently typed programming
  • Programming language comparisons
Hidden categories:
  • Articles with short description
  • Short description matches Wikidata
  • Articles with example C++ code
  • Articles with example C Sharp code
  • Articles with example Java code
  • Articles with example OCaml code
  • Articles with example Python (programming language) code
  • Articles with example Rust code
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.