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
0
votes
1 answer

Generic method for Excel.Workbook and Excel.Worksheet

I'm writing an Excel Add-in and for 1 of the functions I need to calculate some summary statistics. These should be added as names to either the entire Workbook or to a single Worksheet. Right now I have 2 methods that do each thing. However though,…
Krowi
  • 1,565
  • 3
  • 20
  • 40
0
votes
2 answers

Why does 'Func' compile while 'Func where TGeneric : IBase' doesn't?

Why is the following bloc wrong? public interface IBase { } public class ClassX : IBase { } public class ClassY { public static ClassX FunctionReturnX() { return new ClassX(); } } public class…
Aly Elhaddad
  • 1,913
  • 1
  • 15
  • 31
0
votes
1 answer

Instantiating a new object of T inside of a generic function in C#

I have two models that can both use the function below. I created an Interface that both of these models implement. In the function below I am looping through the passed in list and using the values in it to create a new version of the list. public…
Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
0
votes
1 answer

Java JDBC - From database to Arraylist

I am looking for a more generic approach to add rows from a database to its corresponding model. Where am i now... I have multiple tables in a database with information about computerparts, ex. cpu(manufacturer, cores, speed ...), which I have added…
0
votes
2 answers

generic programming in C - void*- const-correctness

My question is, if it is correct to define both: typedef void* Elem; typedef const void* const constElem; If I know that I would work with const and non const generic elements, for example for the copyElem function as a parameter, I would prefer…
0
votes
0 answers

assign a generic type to a multiple class java

I have a class called ExportExcelCell that I assign to a generic type package com.morningstar.portfolioservice.domain.export; import com.fasterxml.jackson.annotation.JsonProperty; import…
joe80
  • 11
  • 4
0
votes
2 answers

Objective-C - Can lightweight generic contain multiple types?

This new language feature comes with iOS9 a few time ago, I knew the basic usage. @property (nonatomic, strong) NSArray *params; But I want to define params as NSArray , the array contains NSString* or…
Itachi
  • 5,777
  • 2
  • 37
  • 69
0
votes
0 answers

How to store template class objects in a container when they use different template parameters?

I have a class with which I'm wrapping some information, for example: template class Type { pubilc: Type(const T value) : m_value(value) {} T GetValue() const { return m_value; } private: T m_value; }; And I want to store a…
NeomerArcana
  • 1,978
  • 3
  • 23
  • 50
0
votes
1 answer

Compare > to Integer

I am new to generics and want to solve a little Task. I want to give two Objects of type "V extends Comparable" to the class ComparePredicate and then check in method "isOk" if the int-value "value" of a Tree class is between these two Objects. I…
Tom Deng Long
  • 527
  • 5
  • 11
0
votes
2 answers

How do I define a function parameter default associated with a generic parameter?

I am attempting to refactor a function (found towards the end of this StackOverflow answer) to make it slightly more generic. Here's the original function definition: def tryProcessSource( file: File, parseLine: (Int, String) =>…
chaotic3quilibrium
  • 5,661
  • 8
  • 53
  • 86
0
votes
4 answers

How to do different things according to type but not using overriding?

For example, I have some Shape, and each shape return different types of buttons: #include struct Button{ Button(){ printf("Button\n"); } }; struct CircleButton : public Button{ CircleButton(){ …
ggrr
  • 7,737
  • 5
  • 31
  • 53
0
votes
1 answer

link error with illegal call

CUtil::input(command); I wrote the code above in "main.cpp" and I made a header file for that code, which is written below. But I received the following error message: C2352: 'class::function' : illegal call of non-static member…
윤성필
  • 85
  • 1
  • 1
  • 7
0
votes
0 answers

improvement of generic linked list

in the generic linked list, where we con store any kind of data but same type of data in the entire linked list and only fixed no of data in the entire linked list. I want such a linked list that can store data belonging to different data types in…
0
votes
1 answer

Dynamically Converting strings into Class Name in same namespace Type.GetType() returning null

I have a List of strings which contains UserControl classes. I wish to pass that List to a foreach() loop and create an object of each class in the List List MyUserControlList= new List(); Permissions.Add("Profile_UC"); …
Muhammad Arslan Jamshaid
  • 1,077
  • 8
  • 27
  • 48