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

Counting frequency of generic object in a binary tree

Im creating a method to count the frequency of a value that is the methods parameter. I have created a binary tree and put values into it, im now trying to count all the values that equals the parameter. The problem is im getting a infinite loop,…
comodeque
  • 95
  • 1
  • 2
  • 7
0
votes
2 answers

generic service and controller spring

package com.lhoussaine.springjsfjpa.entities; @Table(name="address") @Entity public class Address { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private…
0
votes
2 answers

Type Safety: Unchecked Cast from Object[] to T[]

When I write generic class in Java, I use below code public ResizableArray() { this.count=0; this.elements = (T[]) new Object[0]; } However I get a warning: Type Safety: Unchecked Cast from Object[] to T[] Why do I get this warning?
vavio
  • 17
  • 2
  • 3
0
votes
0 answers

Dynamically Loading Classes with Generic Supertypes

The purpose of my code below is to call the initialize() method all of the classes in a package that extend GsonLoader. The error which I am receiving is: java.lang.InstantiationException: com.salesy.utility.gson.impl.NPCSpawnsLoader at…
Tyluur
  • 91
  • 1
  • 2
  • 9
0
votes
2 answers

how can we use the changeable variable as a switch case label

how can we use the changeable variable as a switch case label. in other words, I have a macro defined. But I need to change this value at run time depending on a condition. How can I implement this? example is given below, Here , case "FOO" will…
0
votes
1 answer

Java Generic Method - Cannot instantiate Class with generic parameter

Following example is not the original code, I'm not looking for a workaround. There is a Class For Generic Parsing/UnMarshalling like: public class UnMarshaller { ... This works fine, until I try to provide a Generic Method to…
MemLeak
  • 4,456
  • 4
  • 45
  • 84
0
votes
1 answer

new Java Generic class constructor issue when templete class nested

All I have below define : JsonResponse.class as below: public abstract class JsonResponse extends AbstractResponse { private T jsonObject; public JsonResponse(HttpResponse httpResponse, Class classT) { super(httpResponse); jsonObject…
Jerry Cai
  • 571
  • 2
  • 8
  • 18
0
votes
1 answer

Generic way to restrict a user's actions on a web application (PHP)

I've been tasked to work out a model which will introduce restrictions or levels in an existing webapplication. The application is not consequently build to easily implement this feature. Serverside is PHP, clientside is jQuery. These restrictions…
Gnagy
  • 1,432
  • 16
  • 26
0
votes
1 answer

Why does this constructor that uses generics fail?

I am creating a class that, at present, stores lists of various types in an internal object called genericTable. Each list (composed of either Double, or Long) are all held in an object which is an instance of class GenericList. Question: Why…
user2763361
  • 3,789
  • 11
  • 45
  • 81
0
votes
1 answer

a generic function example using output iterator not working

I have the following template function: template Out copy(In begin, In end, Out dest) { while(begin != end) *dest++ = *begin++; return dest; } and when I call the following: static const double arr[] = {50,23,…
ArmenB
  • 2,125
  • 3
  • 23
  • 47
0
votes
1 answer

generalized sorting function and using binary_function

template struct gSorting : public std::binary_function { bool operator() (int number, int n2) { cout << "int" << endl; return (number
aah134
  • 860
  • 12
  • 25
0
votes
2 answers

Approach to this Generic Programming Scenario with Java's Paramerterized Types and Polymorphism

Say I have an interface: public interface Foo { public void setId(int id); public int getId(); } And objects that implement that interface: public class Bar implements Foo { ... } ; I wish to have a generic function that creates a Map in the form…
0
votes
3 answers

Swap every two bits of an unknown variable type

I need to swap every two adjacent bits as a generic operation which will do so for every variable type given. I thought of the needed masks and operations for every single byte: (var & 0x55) << 1 | (var & 0xAA) >> 1 But how do I make this apply for…
Quaker
  • 1,483
  • 3
  • 20
  • 36
0
votes
1 answer

Using a multi-dimensional array with a generic function

I've written the following generic function: void* scramble(void* arr, int ElemSize, int n, int* indArr){ void * res = malloc(n * ElemSize); int i; for (i = 0 ; i < n ; i++) memcpy((BYTE*)res+i*ElemSize,…
David Tzoor
  • 987
  • 4
  • 16
  • 33
0
votes
1 answer

(Play 2.1.3) minimize the code in models. Generics for static methods

Is there any way not to describe every model in Play? When using Ebean I wind up writing the definition of the Finder in every model: public static Finder find = new Finder( Long.class, Task.class ); also I usually…
alexg
  • 902
  • 11
  • 37