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

Need help about wildcard generic types in Java

I am learning generic programming in java. I saw these charts in Core Java (Edition 9): And I saw these charts in Introduction to Java Programming Comprehensive Version Tenth Edition: But I believe it should be this instead: Could someone tell…
cmpltrtok
  • 323
  • 3
  • 9
6
votes
4 answers

What kinds of types does qsort not work for in C++?

std::sort swaps elements by using std::swap, which in turn uses the copy constructor and assignment operators, guaranteeing that you get correct semantics when exchanging the values. qsort swaps elements by simply swapping the elements' underlying…
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
6
votes
1 answer

Shapeless Witness and how it can give the actual singleton type

I'm trying to understand singleton types in shapeless and faced misunderstanding about singleton types compile-time type. Here is an example: val x: Witness.`120`.T = 120.narrow It works fine, but this constructions looks very unusual. What is…
Some Name
  • 8,555
  • 5
  • 27
  • 77
6
votes
2 answers

How to get Mypy working with multiple mixins relying on each other?

Currently in Electrum we use the Union type on self to be able to access methods from multiple mixed-in parent classes. For example, QtPluginBase relies on being mixed into a subclass of HW_PluginBase to work. For example, a valid use is class…
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
6
votes
2 answers

Creating tables on demand with Spring Boot Data JPA

I'm trying to create a Spring Boot application where I want to create and use database tables on demand, without defining entities / repositories for them. I have got a base Business entity, and BusinessType entity to keep the type of business. I…
ikbal
  • 1,844
  • 4
  • 26
  • 46
6
votes
2 answers

Using Typeable to partially apply function at run-time (any time types match)

Generic programming time! If I have a function: f :: a1 -> a2 -> a3 -> ... -> an and a value v :: aX -- where 1 <= x < n Without knowing at compile time which of the arguments of f the value v is the right type for (if any), can I partially…
Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
6
votes
3 answers

How to make controller endpoint to get two different objects in java spring?

I have a server built with java and spring. What i am trying to do is that my controller with the same endpoint will get two different objects. This is an example for what I mean: I know I can do that: public class Option1{ private String…
JJ Redikes
  • 421
  • 6
  • 19
6
votes
1 answer

How can I implement a function differently depending on if a generic type implements a trait or not?

I'd like to make the implementation of do_something conditional based on if the generic type T implements Debug or not. Is there any way to do something like this? struct A(i32); #[derive(Debug)] struct B(i32); struct Foo { data: T, /*…
6
votes
1 answer

generic type alias, which are incompatible to each other

I am trying to construct some kind of "generic type alias", meaning I want to define a type as int for example, but with a generic type argument which then makes it incompatible with instances of other types. I tried doing this with alias…
Felk
  • 7,720
  • 2
  • 35
  • 65
6
votes
6 answers

Using templates instead of bridge pattern in C++

I have three types of data link: RS485, I2C and Bluetooth. Every data link have functions like connect, read and write data. On PC software I must implement application/protocols layers to work with devices. In my previous question about OOP I got…
Tomag
  • 101
  • 7
6
votes
1 answer

How can I use generics to extract all values of a particular type?

I have a data type like this: data MyType = Foo Bool | Bar | Baz Bool (Maybe String) Bool | Quux Int String Can I use generics to write a function getBools :: MyType -> [Bool] that returns a list of all the…
Echo Nolan
  • 1,111
  • 1
  • 11
  • 23
6
votes
1 answer

Are there any ways to recursively flatten tuples?

In Rust, is there any way to use traits and impls to (recursively) flatten tuples? If it helps, something that works with N nested pairs is a good start trait FlattenTuple { fn into_flattened(self) -> /* ??? */ } // such that assert_eq!((1, (2,…
lloydmeta
  • 1,289
  • 1
  • 15
  • 25
6
votes
1 answer

Swift generics postponed issue

I'm trying to do this but I'm getting some troubles This is CustomProtocol protocol CustomProtocol { } SubCustomProtocol protocol SubCustomProtocol: CustomProtocol { } SubCustomProtocolImplementation class SubCustomProtocolImplementation:…
Roberto Frontado
  • 438
  • 5
  • 16
6
votes
2 answers

Is there a generic function that takes a data structure and returns all ints in it?

I think the type signature would look like f :: a -> [Int] input data would look like data NamedPoint = NamedPoint String Int Int data Person = Name Int Int Int and using it in the REPL would look like this: >> let namedPoint = NamedPoint "hey" 1…
user514156
  • 630
  • 1
  • 7
  • 14
6
votes
3 answers

C# Extension Method on Type With Generic Type Argument

I’m looking at ways to improve the consistency, brevity, and readability of some code in the application I’m working on. The starting code looked something like this: context.GetGraphType().Subscribe( (instance, evt) =>…
Bryan Matthews
  • 1,116
  • 1
  • 8
  • 13