Questions tagged [generic-programming]

A style of programming in which algorithms are implemented abstracting from concrete data types. Usually referred to strongly typed languages this term is usually treated as creating software which is minimal in terms of data type requirements and can be instantiated for each conforming data type without changing the callee code.

This tag should be used for questions on applying generic programming techniques. Language specific features (, ) related to issues that are not directly connected with generic programming should not be marked with this tag.

Core ideas of generic programming (GP)

  1. Lifting of an algorithm stands for finding minimal requirements for a data type interface which allow to implement the algorithm. Usually this improves reusability by widening of range of conforming data types. This process reduces coupling of the software modules and reduces dependence on secondary aspects, which typically leads to cleaner code.
  2. Specification is a process of constraining data types, typically to facilitate more efficient implementation of an algorithm.
  3. Concept stands for an interface of a data type which is an object of the lifting and specification processes.
  4. Reusable, efficient code generally depends on a balance between lifting and specification.

Widely known implementations

  1. Standard Template Library (STL) which was created by Alexander Stepanov, a GP pioneer.
  2. Generics in Java and
  3. Generics in .NET

Although less known, the first relatively widely used programming language to provide direct support for generic programming was Ada 83.

1288 questions
169
votes
9 answers

Generics/templates in python?

How does python handle generic/template type scenarios? Say I want to create an external file "BinaryTree.py" and have it handle binary trees, but for any data type. So I could pass it the type of a custom object and have a binary tree of that…
keys
  • 1,693
  • 2
  • 11
  • 4
106
votes
6 answers

How can I check for generic type in Kotlin

I'm trying to test for a generic type in Kotlin. if (value is Map) { ... } But the compiler complains with Cannot check for instance of erased type: jet.Map The check with a normal type works well. if (value is String) { ...…
phil
  • 1,377
  • 2
  • 9
  • 14
73
votes
4 answers

What is Haskell's Data.Typeable?

I've come across references to Haskell's Data.Typeable, but it's not clear to me why I would want to use it in my code. What problem does it solve, and how?
Muin
  • 1,251
  • 11
  • 8
51
votes
4 answers

Tag dispatch versus static methods on partially specialised classes

Suppose I want to write a generic function void f(), which does one thing if T is a POD type and another thing if T is non-POD (or any other arbitrary predicate). One way to achieve this would be to use a tag-dispatch pattern like the standard…
49
votes
1 answer

What does "typename =" mean in the template parameters?

I have seen this expression in page 189 of the book "Effective Modern C++": template::type> explicit Person(T&& n); I am just wondering what does the part "typename ="…
qft
  • 705
  • 6
  • 9
44
votes
2 answers

How do I build gcc with C++ concepts ("concepts lite") support?

The C++ standards committee is working on a TS (Technical Specification) for Concepts extension: "Programming Languages - C++ Extensions for Concepts". N4377 is the latest version of this document. For inclusion into the C++ standard features are…
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
42
votes
4 answers

Template function as a template argument

I've just got confused how to implement something in a generic way in C++. It's a bit convoluted, so let me explain step by step. Consider such code: void a(int) { // do something } void b(int) { // something else } void function1() { …
Kos
  • 70,399
  • 25
  • 169
  • 233
38
votes
1 answer

Is there any trait that specifies numeric functionality?

I'd like to use a trait to bound a generic type, like this hypothetical HasSQRT: fn some_generic_function(input: &T) where T: HasSQRT, { // ... input.sqrt() // ... }
Hossein Noroozpour
  • 1,091
  • 1
  • 13
  • 26
29
votes
3 answers

nested-name-specifier

I have a code like: namespace mymap { template class Allocator> myownmap { typedef pair typename _myPair; typedef multimap ,Allocator<_myPair>…
akashihi
  • 917
  • 2
  • 9
  • 19
29
votes
4 answers

Why don't the Haskell standard libraries make more use of polymorphism?

I'm in the process of learning Haskell, and type classes seem like a powerful way to make type-safe polymorphic functions. But a lot of the Haskell Prelude functions don't use them. More specifically: Most of the list functions don't work with…
shosti
  • 7,332
  • 4
  • 37
  • 42
28
votes
9 answers

'Multipurpose' linked list implementation in pure C

This is not exactly a technical question, since I know C kind of enough to do the things I need to (I mean, in terms of not 'letting the language get in your way'), so this question is basically a 'what direction to take' question. Situation is: I…
Rafael Almeida
  • 10,352
  • 6
  • 45
  • 60
28
votes
4 answers

Boilerplate-free annotation of ASTs in Haskell?

I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "annotation" to certain nodes, such as tail-calls, etc. I…
27
votes
6 answers

C++ vs. D , Ada and Eiffel (horrible error messages with templates)

One of the problems of C++ are horrible error messages that we are getting from code which intensively uses templates and template metaprogramming. The concepts are designed to solve this problem, but unfortunately they will not be in the next…
UmmaGumma
  • 5,633
  • 1
  • 31
  • 45
25
votes
3 answers

Haskell: Get data constructor name as string

Let us say we have data D = X Int | Y Int Int | Z String I wish to have a function getDConst getDConst :: D -> String that returns either "X", "Y", or "Z", according to the data constructor used for its input. Is there a generic way to write this…
tohava
  • 5,344
  • 1
  • 25
  • 47
24
votes
5 answers

Can overloads for generic functions be open for other overloads?

I want to implement some generic algorithms and I have a number of ideas how specialized algorithms could be implemented depending on certain traits of entities the algorithm is used with. However, it seems likely that I didn't come up with all…
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1
2 3
85 86