LLMpediaThe first transparent, open encyclopedia generated by LLMs

Generic programming

Generated by DeepSeek V3.2
Note: This article was automatically generated by a large language model (LLM) from purely parametric knowledge (no retrieval). It may contain inaccuracies or hallucinations. This encyclopedia is part of a research project currently under review.
Article Genealogy
Parent: TypeScript Hop 4
Expansion Funnel Raw 57 → Dedup 34 → NER 5 → Enqueued 5
1. Extracted57
2. After dedup34 (None)
3. After NER5 (None)
Rejected: 29 (not NE: 29)
4. Enqueued5 (None)

Generic programming. A style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. This approach, central to writing reusable and type-safe libraries, emphasizes generality and efficiency by separating data structures from the algorithms operating on them. Its development is closely associated with the work of Alexander Stepanov and the creation of the Standard Template Library for C++.

Overview

The fundamental principle is to design functions and data structures that operate on any data type meeting specific semantic requirements, rather than a single type. This paradigm promotes code reuse and enhances type safety by shifting certain checks from run time to compile time, as seen in languages like C++ and Rust. Pioneering implementations, such as those in the Standard Template Library, demonstrated that generics could deliver both abstraction and performance rivaling hand-coded, type-specific versions. The paradigm has profoundly influenced the design of major programming languages and is a cornerstone of modern software engineering.

Language support

Many mainstream languages implement this paradigm, though with differing syntax and semantics. C++ utilizes templates, a powerful but complex compile-time mechanism foundational to its standard library. Java and C# employ a more constrained, runtime-aware system known as generics, which use type erasure and reification, respectively. Rust features a sophisticated system combining traits and generic type parameters, while Haskell achieves similar goals through its type class system. Other languages with support include Swift, TypeScript, and Go.

Concepts and abstractions

Core to the paradigm are abstract concepts that define requirements on types. In C++, these are formalized as concepts (since C++20) or historically as named requirements in documentation. Rust expresses these constraints via traits, and Haskell uses type classes. Key abstractions often include requirements like being Comparable, Copyable, or DefaultConstructible, which ensure types support specific operations like comparison or duplication. These mechanisms enable compile-time polymorphism and are essential for defining algorithms that work across diverse types, from integers to custom classes.

Comparison with other paradigms

It contrasts with object-oriented programming, which achieves polymorphism through inheritance and virtual function tables, often incurring runtime overhead. While OOP is based on explicit type hierarchies, generic programming relies on implicit, structural constraints defined by required behaviors. It also differs from duck typing found in languages like Python, where type checking occurs entirely at runtime. Compared to macro systems, as in C, generics provide stronger type safety and better integration with the language's type system.

Implementation techniques

Different languages use distinct compilation strategies. C++ templates rely on compile time code generation, creating a new instantiation of the template code for each type used, which can lead to code bloat but allows for deep optimization. Java uses type erasure, where generic type information is removed after compilation, maintaining compatibility with the Java Virtual Machine but limiting certain operations. C# and Rust preserve type information at runtime through reification, enabling features like reflection and pattern matching. Advanced techniques include template metaprogramming in C++ and higher-kinded polymorphism in Haskell.

History and development

The theoretical foundations were significantly advanced by Alexander Stepanov, who, while working at Hewlett-Packard and later at SGI, developed the Standard Template Library. The STL was proposed for and incorporated into the ANSI C++ standard in the 1990s. Early influences include the polymorphic functions in ML and the generic modules in Ada. The paradigm's adoption accelerated with its inclusion in the .NET Framework and the Java Platform, Standard Edition. Landmark developments include the addition of concepts to C++20 and the design of the Rust ownership system, which integrates generics with memory safety guarantees.

Category:Programming paradigms