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

Swift - How to write generic max function

I'm new to Swift and I want to write a generic max function which compares the two parameter and returns the larger one, for basic types like Int, Double, etc. func max(_ num1:T, _ num2:T) -> T { return (num1 > num2) ? num1 : num2; } But I…
keanehui
  • 185
  • 2
  • 13
-3
votes
1 answer

How to add values to a collection object in c#?

How to add values to a collection object in c#? I am trying to add list of values which are in the incremental sequence to a collection object. Sample syntax :- Source Data FirstName LastName Department …
goofyui
  • 3,362
  • 20
  • 72
  • 128
-3
votes
1 answer

Avoid repetitive coding to transfer local variable to an object field

I have a DBReader class which reads the data from multiple databases. I have a DBDataCache class which is meant to cache data for a specific database, and has member variables, like ArrayList arlCostCentres, DataTable dtPriceLists, etc. The DBReader…
AllSolutions
  • 1,176
  • 5
  • 19
  • 40
-3
votes
1 answer

Converting into Generic class

I have trouble understanding generics concept. I need to convert class DataSet into its generic form. I especially don't get what to do with the fields of DataSet. I do understand that we have to substitute all the signatures with T. /** Computes…
-3
votes
1 answer

How to invoke a method with generic return type in java

I want to have a method returning a generic type. Let's say I have a parent class and 10 child classes. The parent class does not have any method defined and the child classes have different functions (none of them in common). I want to create an…
Kunal Shah
  • 19
  • 5
-3
votes
3 answers

Dynamically generate object field of generic type

Code: objectType request = factory.create(); public class factory { public static T create() where T : new() { T obj = new T(); PropertyInfo propertyInfo = obj.GetType().GetProperty("client_no"); …
Demodave
  • 6,242
  • 6
  • 43
  • 58
-3
votes
2 answers

Generic Array Java

/** * Compares this array with another array. *

* This is a requirement of the Comparable interface. It is used to provide * an ordering for Array elements. * @return a negative value if the provided array is "greater than" this array, *…

nurul
  • 1
-4
votes
2 answers

Cannot pass Object which extend of a abstract one into a Method with the abstract Object as Parameter

Here's the code of the method: public void setupList(ArrayList list) { //Here´s the Code stuff. This Method is in the Class: AlphabeticList } And I tried to call this method with: setUpList(new ArrayList); The…
-4
votes
2 answers

Order of Complexity of customized tree

Please tell me the complexity of below tree. Please explanation the procedure of calculating it also. The tree structure: root->left->right and root->right->left are pointing to same node. Algorithm: If we traverse the tree with normal inorder…
Geek
  • 11
  • 1
-4
votes
1 answer

C# Extending generic method

Good day, Is there a way to extend generic method? for example i have such method: public T DoSomethingAboutIt() { //do magic } what i want to is to have extended method such as: private static T Extended(this T o, Func func) { …
Weazel
  • 63
  • 6
-5
votes
3 answers

How to convert generic potentially nested map Map[String, Any] to case class using any library in Scala?

I've not had much joy with reflection, this answer using shapeless works for some cases (but seems to have many edge cases) Shapeless code to convert Map[String, Any] to case class cannot handle optional substructures Does anyone know of a nice…
samthebest
  • 30,803
  • 25
  • 102
  • 142
-5
votes
1 answer

Java Generic Function

Can we write generic function in java to add different types of variable. Example: 2+3 = 5(Integer addition) "hi" + "I am here" = "hi I am here" (String addition)
-5
votes
2 answers

Generics - How can i put this code into Generics

How can i put this code into Generic form ? List aviary = new ArrayList(); Eagle any Eagle; aviary.add(new Eagle(100, "Brutus")); aviary.add(new Eagle(100, "Chronos")); for (int i=0; i
1 2 3
85
86