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
5
votes
1 answer

How to extend from AsyncTask as a generic base class?

There are three different implementations of an AsyncTask: public class DownloadTask extends AsyncTask public class JsonParserTask extends AsyncTask public class PostCommentTask extends…
JJD
  • 50,076
  • 60
  • 203
  • 339
5
votes
3 answers

Whether to choose an overload of template function or a partial specialization of a functor(function object)

The following is an excerpt of STL implementation of g++ (the sgi version of STL). I want to know why they use partial specialization instead of function overload. template struct __copy_dispatch { …
Joey.Z
  • 4,492
  • 4
  • 38
  • 63
5
votes
1 answer

Couldn't match kind `*' against `#'

What the heck is going on here: "Couldn't match kind `*' against `#'" I was trying the following in GHCi using TemplateHaskell (ghci -XTemplateHaskell) $(reify ''Show >>= dataToExpQ (const Nothing)) I was hoping to get an Exp out of this (which…
scravy
  • 11,904
  • 14
  • 72
  • 127
5
votes
2 answers

Haskell's Scrap Your Boilerplate (SYB) - applying transformation only once instead of everywhere

What's the best way to apply a transformation to a tree only once instead of everywhere using SYB? For instance, in the following simplified expression, there are several instances of Var "x", and I want to replace the first instance with Var "y"…
5
votes
2 answers

C++: Pure virtual type

I'm exploring template shenanigans in C++ (C++11), and one thing I'd like to have is a pure virtual type in an abstract class. This would be like Scala's abstract types. In C++ I'd want to do something like the following: struct Base { // Says any…
emchristiansen
  • 3,550
  • 3
  • 26
  • 40
5
votes
3 answers

Increase compile-time variable with every instantiation of a generic class

I have this class: template struct Probe { static const uint64_t Counter = N; typedef T Type; }; Which I utilize as: typedef Probe FirstIntProbe; typedef Probe SecondIntProbe; typedef Probe
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
5
votes
1 answer

How to define SYB functions for type extension for tertiary type constructors (ext3)?

In the Scrap Your Boilerplate package, in Data.Generics.Aliases, there are functions to allow type extension for unary, and binary type constructors. In particular, there are definitions for ext1 and ext2. Now, ext1 and ext2 are defined in terms of…
scvalex
  • 14,931
  • 2
  • 34
  • 43
5
votes
1 answer

Generic functions for standard deviation in monte carlo C++

I am supposed to compute the standard deviation function in some monte carlo simulations. The formula is this one: I think my results are way off what they should be. My function uses tuples from the boost library and it looks like this: double…
Mathias
  • 69
  • 4
5
votes
2 answers

Determining return type of "generic function"

Suppose, I want to develop a generic library which should be usable with number-like types including double and user-defined types. The problem, I'm facing right now is that I don't know how to write the return type of a function template much like…
5
votes
1 answer

Algorithm to resolve dependencies and build tree

Google search did not yield any ideas or solutions neither did SO search.( One post existed with same title but different motive). Are there well known algorithms, patterns to resolve dependencies and build a hierarchy of objects. It must be able…
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
4
votes
4 answers

Recursive generic function used as a predicate, compilation failure

I am writing a function to compare the contents of two lists. The order of the elements don't matter, so I sort them before I compare. The lists can be of normal types list, but also be lists of lists list >. Here is a complete…
knatten
  • 5,191
  • 3
  • 22
  • 31
4
votes
1 answer

Rust: How to restrict type parameters for derived traits

I'm trying to write a generic function that takes a Path pointing to a csv file, parses and deserializes the file to a vector of records of a certain type and returns the vector of records. Here is my code: [dependencies] csv = "1.1" serde = {…
pragMATHiC
  • 388
  • 5
  • 9
4
votes
1 answer

Overload regardless noexcept specification

I have to provide an overload set of f, that accepts both member and member function pointers: void g(int) {} template void f(const T& t, Field T::*field) { g(t.*field); } template void…
erenon
  • 18,838
  • 2
  • 61
  • 93
4
votes
3 answers

Java compiler not able to infer type on generic chain

As mentioned below (to remark it as I might have explained myself badly): I want to understand the principle behind this issue so that I can apply that knowledge to the real problem. ISSUE START I am working in a system intended to be an abstract…
4
votes
2 answers

How to filter record keys based on their values?

I have this record: interface TheRecord extends TheRecordType { a: { typeA: 'string' }, b: { typeB: 123 }, c: { typeA: 'string' }, } type TheRecordType = Record type TypeA = { typeA: string } type TypeB = { typeB:…