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
10
votes
2 answers

Why doesn't C++ make it easier to make compile time queries on types?

I'm just beginning to learn template metaprogramming tricks that allow you to query about a type. For example, SFINAE allows us to check if a type has a particular typedef or function at compile time by using overloading and return type sizeof…
Raja
  • 2,846
  • 5
  • 19
  • 28
10
votes
2 answers

Container with proxy iterator/reference and auto

I'm implementing a container with a proxy iterator/reference type similar to std::vector and clash into the following issue, which I proceed to exemplify with std::vector (this question is not about std::vector!): #include…
gnzlbg
  • 7,135
  • 5
  • 53
  • 106
10
votes
4 answers

In C, Generic Containers or Safe Containers?

In C++ you can have both generic and type safe containers by using templates. However in C, if you want generic containers, you have to (afaik) use void*, which means you lose type safety. To have type safe containers, you would have to reimplement…
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
9
votes
5 answers

Avoiding Java Type Erasure

Is there a way one could avoid type erasure and get access to a type parameter? public class Foo & Bar> { public Foo() { // access the template class here? // i.e. : baz(T.class); // obviously doesn't…
Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
9
votes
2 answers

Is noreturn part of the signature of a function?

[dcl.attr.noreturn] can be used to mark that a function doesn't return. [[ noreturn ]] void f() { throw "error"; } Is [[noreturn]] part to the identity/signature of a function? can one detect that a function is noreturn at the time of…
alfC
  • 14,261
  • 4
  • 67
  • 118
9
votes
1 answer

Generalizing fold such that it becomes expressive enough to define any finite recursion?

So, there is something known as a "universal property of fold", stating exactly following: g [] = i; g (x:xs) = f x (g xs) <=> g = fold f i However, as you probably now, there are rare cases like dropWhile, which can not be redefined as fold f i…
Zazaeil
  • 3,900
  • 2
  • 14
  • 31
9
votes
0 answers

Generic GHC rewrite rules

I recently added an alterF function to Data.Map, which is a flipped form of Control.Lens.At.at. alterF :: (Ord k, Functor f) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a) alterF is designed to be able to…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
9
votes
4 answers

Is it possible to partially apply nth parameter in Haskell?

I am curious if it is possible to write a function apply_nth that takes a function, the number of a parameter, and that parameter's value and then returns a new, partially-applied function. The feeling I get is that this is impossible due to the…
9
votes
3 answers

Grouping data types by constructor in Haskell

Given this data type data Val = X Int | Y Bool | Z Double deriving (Eq, Show) and a list such as let vals = [X 1, Z 2.7, Y True, X 2, Z 3.14, Y True] how to group elements in vals into this list, [[X 1,X 2],[Y True,Y True],[Z 2.7, Z 3.14]]
elm
  • 20,117
  • 14
  • 67
  • 113
9
votes
4 answers

Traversing and filtering a tree in haskell

I am pretty new to Haskell (still working on totally understanding monads). I have a problem where I have a tree like structure type Tree = [DataA] data DataA = DataA1 [DataB] | DataA2 String | DataA3 String [DataA] …
Chris
  • 483
  • 3
  • 14
9
votes
1 answer

Deriving default instances using GHC.Generics

I have a typeclass Cyclic for which I would like to be able to provide generic instances. class Cyclic g where gen :: g rot :: g -> g ord :: g -> Int Given a sum type of nullary constructors, data T3 = A | B | C deriving (Generic,…
cdk
  • 6,698
  • 24
  • 51
9
votes
1 answer

Avoiding boilerplate when dealing with many unrelated types

I'm writing code that deals with values from Language.Exts.Annotated.Syntax, where a variety of types are defined that mirror the structure of a Haskell module: data Module l = ... data Decl l = ... data Exp t = ... -- etc I'd like to be able to…
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
9
votes
10 answers

Developing for Mac OS X, on Windows?

Well, simple situation. I happen to be a software engineer who uses mostly Delphi and C# for software development. Delphi is great for desktop applications while C# is ideal combined with ASP.NET for web applications. However, I am considering to…
Wim ten Brink
  • 25,901
  • 20
  • 83
  • 149
9
votes
5 answers

Why does "as T" get an error but casting with (T) not get an error?

Why can I do this: public T GetMainContentItem(string moduleKey, string itemKey) { return (T)GetMainContentItem(moduleKey, itemKey); } but not this: public T GetMainContentItem(string moduleKey, string itemKey) { return…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
9
votes
1 answer

Transposing arbitrary collection-of-collections in Scala

I have to often transpose a "rectangular" collection-of-collections in Scala, e.g.: a list of maps, a map of lists, a map of maps, a set of lists, a map of sets etc. Since collections can be uniformly viewed as a mapping from a specific domain to a…