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

How much existing C++ code would break if void was actually defined as `struct void {};`

void is a bizarre wart in the C++ type system. It's an incomplete type that cannot be completed, and it has all sort of magic rules about the restricted ways it can be employed: A type cv void is an incomplete type that cannot be completed; such a…
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
22
votes
1 answer

What are concepts?

I've heard all this new (on /.) about C++0x not having concepts anymore, but I have no idea what they are? Can someone explain to me?
devin
  • 6,407
  • 14
  • 48
  • 53
21
votes
6 answers

Generic programming vs. Metaprogramming

What exactly is the difference? It seems like the terms can be used somewhat interchangeably, but reading the wikipedia entry for Objective-c, I came across: In addition to C’s style of procedural programming, C++ directly supports certain…
20
votes
1 answer

Indexing into containers: the mathematical underpinnings

When you want to pull an element out of a data structure, you have to give its index. But the meaning of index depends on the data structure itself. class Indexed f where type Ix f (!) :: f a -> Ix f -> Maybe a -- indices can be out of…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
20
votes
1 answer

Generic programming via effects

In the Idris Effects library effects are represented as ||| This type is parameterised by: ||| + The return type of the computation. ||| + The input resource. ||| + The computation to run on the resource given the return value. Effect : Type Effect…
effectfully
  • 12,325
  • 2
  • 17
  • 40
19
votes
3 answers

How to use abstraction with ViewBinding with base activity?

I was making a base class so that all bindings for child will be set in base I have done till this abstract class BaseActivity2 : AppCompatActivity() { private var viewBinding: B? = null private var…
19
votes
2 answers

TypeScript: How to deal with generic types and the keyof operator

I try to write a generic function which assembles update data for database updates. Passed arguments: record to be updated property key a new array item Even though I restrict the key's type using keyof R, I cannot assign a new object with that…
ideaboxer
  • 3,863
  • 8
  • 43
  • 62
19
votes
4 answers

Can someone explain what does mean and when should it be used and how this construction should cooperate with and ?

I'm using generics rather long time but I've never used construction like List. What does it mean? How to use it? How does it look after erasure? I also wonder: is it something standard in generic programming (template programming?) or…
Roman
  • 64,384
  • 92
  • 238
  • 332
19
votes
3 answers

Why is std::less a class template?

According to 20.8.5 §1, std::less is a class template with a member function: template struct less { bool operator()(const T& x, const T& y) const; // ... }; Which means I have to mention the type when I instantiate the…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
18
votes
9 answers

When/Why ( if ever ) should i think about doing Generic Programming/Meta Programming

IMHO to me OOPS, design patterns make sense and i have been able to apply them practically. But when it comes to "generic programming /meta programming" of the Modern C++ kind, i am left confused. -- Is it a new programming/design paradigm ? -- Is…
Aman Aggarwal
  • 3,905
  • 4
  • 26
  • 38
18
votes
17 answers

What undergraduate computer science course best prepares programmers for the workplace?

The idea here is to get better programmers right out of college. I think I would have to go with Algorithms, it's not exactly something you can pick up on your own very easily and I think it enables you to look at efficiency and correctness of…
Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
18
votes
2 answers

The order of template specializations in C++

Can the order in which template specializations appear in the code alter the meaning of the program? If so, then why? Example: Somewhere inside a source code // specialization A ... // specialization B ... vs. // specialization B ... //…
user6646922
  • 497
  • 1
  • 6
  • 15
18
votes
1 answer

What is "Scrap Your Boilerplate"?

I see people talking about Scrap Your Boilerplate and generic programming in Haskell. What do these terms mean? When would I want to use Scrap Your Boilerplate, and how do I use it?
17
votes
6 answers

How can I subtract two generic objects (T - T) in C# (Example: DateTime - DateTime)?

I wrote a Generic Class: public class Interval where T : IComparable // for checking that Start < End { public T Start { get; set; } public T End { get; set; } ... } And I use this class with DateTime, int, etc. I need a Duration…
Reza ArabQaeni
  • 4,848
  • 27
  • 46
17
votes
17 answers

Generic pair class

Just attempting this question I found in a past exam paper so that I can prepare for an upcoming Java examination. Provide a generic class Pair for representing pairs of things. The class should provide a constructor, a method for getting the first…
John Curtsy
  • 541
  • 3
  • 8
  • 15
1
2
3
85 86