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

Making two similar functions generic

I have two functions in Go that do almost the same. They take a slice of structs which has an 'ID' field, and re-sort it into a map indexed by that field. They then append it to a field of another struct, which is also identified by the ID. The two…
John Magistr
  • 872
  • 3
  • 9
  • 22
4
votes
1 answer

C++ Why does error "no matching function" appear when it 100% looks like they match?

I don't understand why I'm getting an error that states my function doesn't match my defined template function. To me, they look exactly the same. Here is the error in my debug: error: no matching function for call to 'mergesort' newVec =…
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
4
votes
1 answer

C++ Concepts: Can I define a concept that is itself a template?

Sorry if the question isn't too clear. I'm not sure the best way to phrase it (feel free to edit!). I think an example would be the most clear: I am attempting to define a Monad concept based off of the Haskell definition. The bind operator (>>=)…
Sam Kellett
  • 1,277
  • 12
  • 33
4
votes
1 answer

LabelledGeneric instance generation on a tagged case class yields me an error in shapeless

I'm trying to get a LabelledGeneric instance from a tagged type coming from another HList (another LabelledGeneric to be exact), but the compiler yields me an error saying that it cannot find the implicit. The exact error is (enabling…
Sandro
  • 103
  • 6
4
votes
4 answers

Abstract class method - To instantiate child class object?

I'm trying to create a matrix library (educational purpose) and have reached an obstacle I'm not sure how to approach with grace. Adding two matrices is a simple task, using a method get() on each each matrices' elements individually. However, the…
B. Lee
  • 191
  • 6
4
votes
1 answer

How to implement a trait for any iterable type?

I'd like to implement a generic method that's callable on any container or iterator that iterates over a specific type, e.g. &[u32], Vec, (0..99u32), etc. The following code does not compile: trait Foo { fn foo(self); } impl Foo for…
Kornel
  • 97,764
  • 37
  • 219
  • 309
4
votes
1 answer

What is an example of a c++ forwarditerator?

I am looking for an example of an iterator which satisfies the limited qualities to be considered a ForwardIterator, while not being anything else in the hierarchy of iterators. I have written a program which prints which of the five iterator…
4
votes
3 answers

Different value depending on type C++

I'd like to have different variable value depending on type of input variable. Code: template int getValue(vector & data) { return something; // There should be 0 for int and 1 for double } Do anyone know how to achieve such a…
miqelm
  • 354
  • 4
  • 13
4
votes
2 answers

Carrying type information in class template instantiation

I need to access type information from a class I used to instantiate another class. Specifically void Beta::do_something() needs to accept parameters of type W, S that were used to instantiate class Alpha. template class…
Florin Gogianu
  • 338
  • 4
  • 10
4
votes
6 answers

Unique classes in generic list

I have a generic class with a generic list in it. I want to ensure that the generic list only contains unique classes. What I have done so far is to compare the class names with reflection (getClass()). But I think that's not a clean solution. Are…
4
votes
1 answer

Possible to generically remove function types from datatype, to allow deriveJSON?

I have several datatypes representing the state of an application. In various places in the datatype, I have embedded functions or monadic actions, eg. data Foo = Foo Int (ActionM String) data Bar = Bar Foo (Maybe Bar) (ActionM ()) I need to encode…
ajp
  • 1,723
  • 14
  • 22
4
votes
1 answer

How to give a type signature to polymorphic functions when PolyKinds is enabled?

When enabling PolyKinds, previously valid type signatures can become invalid. The following code compiles without PolyKinds. {-# LANGUAGE KindSignatures #-} import GHC.Generics foo :: Constructor c => t c (f :: * -> *) a -> [Char] foo =…
user1078763
  • 728
  • 5
  • 15
4
votes
2 answers

Common lisp typecase vs defgeneric runtime analysis

I'm writing an app with common lisp that uses opengl, and as the thing has grown I've realized I have a bit of a choice to make. I have a bunch of different classes which all need code to render them quickly and often, so I'm considering the…
user4861515
4
votes
2 answers

Refactoring a "dumb" function into generic STL-style with iterators to containers

I've managed to wrap my head around some of C++'s functional capacities (for_each, mapping functions, using iterators...) but the construction of the templates and function argument lists for taking in generic containers and iterators still eludes…
Shamster
  • 2,092
  • 5
  • 24
  • 27
4
votes
4 answers

Using pointers, references, handles to generic datatypes, as generic and flexible as possible

In my application I have lots of different data types, e.g. Car, Bicycle, Person, ... (they're actually other data types, but this is just for the example). Since I also have quite some 'generic' code in my application, and the application was…
Patrick
  • 23,217
  • 12
  • 67
  • 130