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
12
votes
5 answers

How do you do generic programming in Haskell?

Coming from C++, I find generic programming indispensable. I wonder how people approach that in Haskell? Say how do write generic swap function in Haskell? Is there an equivalent concept of partial specialization in Haskell? In C++, I can partially…
obecalp
  • 1,721
  • 1
  • 12
  • 15
12
votes
7 answers

What do you feel is over-generalization?

Having spent some time playing around in Haskell and other functional languages, I've come to appreciate the simplicity of design that comes from describing problems in general terms. While many aspects of template programming can be far from…
Cogwheel
  • 22,781
  • 4
  • 49
  • 67
12
votes
2 answers

Datatype-generic programming libraries for Scala

I'm looking for a Scala library allowing for datatype-generic programming (like Scrap Your Boilerplate, for example). A list of libraries with appropriate links and short descriptions for each one would be a perfect answer.
Marat Salikhov
  • 6,367
  • 4
  • 32
  • 35
12
votes
11 answers

What are the disadvantages of using templates?

Some of the disadvantages would be its syntax is complex compiler generates extra code
anish
  • 1,035
  • 4
  • 13
  • 27
12
votes
3 answers

Generic method to return Nullable Type values

I wrote below method with follwing requirement - input is xmlnode and attributeName return the value if it is found with the associated attribute name passed Where there is no value in attributeName being passed, it should return - 3.1. For int …
CuriousBenjamin
  • 709
  • 1
  • 9
  • 26
11
votes
4 answers

Why were concepts (generic programming) conceived when we already had classes and interfaces?

Also on programmers.stackexchange.com: I understand that STL concepts had to exist, and that it would be silly to call them "classes" or "interfaces" when in fact they're only documented (human) concepts and couldn't be translated into C++ code at…
Gui Prá
  • 5,559
  • 4
  • 34
  • 59
11
votes
2 answers

What's the difference between a trait's generic type and a generic associated type?

This question is asked before generic associated types are available in Rust, although they are proposed and developed. My understanding is that trait generics and associated types differ in the number of types which they can bind to a…
CodeSandwich
  • 1,601
  • 13
  • 23
11
votes
1 answer

Scrap Your Boilerplate in f#

I've used the Scrap Your Boilerplate and Uniplate libraries in the Haskell programming language, and I would find that form of generic programming over discriminated unions to be really useful. Is there an equivalent library in the f# programming…
10
votes
1 answer

Using placement new in generic programming

When using placement new in generic code to construct an object at a specified address, the usage pattern is a bit different from usual code. For example, consider this implementation of uninitialized_copy: ([uninitialized.copy]) template
L. F.
  • 19,445
  • 8
  • 48
  • 82
10
votes
2 answers

Generic Repository pattern for .net core with Dapper

I followed a tutorial on Generic Repository Pattern with ASP.NET core with EF CORE, here for example public class Repository : IRepository where T : class { protected readonly DbContext _dbContext; protected readonly DbSet
10
votes
5 answers

Passing a generic class to a form

I can't seem to find out the answer to this through searching, so here goes.... I know that I can pass Class objects generically to other classes by utilising this type of code: public class ClsGeneric where TObject : class { public…
Davy C
  • 639
  • 5
  • 16
10
votes
3 answers

Is it possible to derive recursion principles generically?

In Idris, there's some magical machinery to automatically create (dependent) eliminators for user-defined types. I'm wondering if it's possible to do something (perhaps less dependent) with Haskell types. For instance, given data Foo a = No | Yes a…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
10
votes
1 answer

Can I use OverlappingInstances to get nicer error messages?

I'm currently dealing with some Haskell code that I didn't write, but that I've made changes to. After my changes, I run the program and get the following error message: Prelude.!!: index too large The call to !! is not in my code, so refactoring…
jmite
  • 8,171
  • 6
  • 40
  • 81
10
votes
3 answers

Difference between add_lvalue_reference_t and T&

Suppose you have a template argument T. What are the differences between add_cv_t and const volatile T add_const_t and const T add_volatile_t and volatile T add_lvalue_reference_t and T& add_rvalue_reference_t and…
Uroc327
  • 1,379
  • 2
  • 10
  • 28
10
votes
3 answers

Template definition of non-template error

I want to use the CRTP pattern in combination with some locking mechanism for access syncing in multithreaded environment. My code looks like this: //-- CRTP base class with some sync/lock mechanism template struct Base { …
Martin Pasko
  • 101
  • 1
  • 7