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 SortByValue based on Numeric

How do I create one implicit class for both use cases? implicit class SortableByIntValue(rdd:RDD[(String,Int)]){ def sortByValue = rdd.sortBy(_._2) } implicit class SortableByDoubleValue(rdd:RDD[(String,Double)]){ def sortByValue =…
Walrus the Cat
  • 2,314
  • 5
  • 35
  • 64
0
votes
1 answer

Can Ruby define some generic operator?

I just learned about Ruby's "spaceship operator", <=>. I find it interesting. Can Ruby define our own special operators like >=< for opposite of <=>? Could it be applied to generic types like Java templates? How do I do it?
user504909
  • 9,119
  • 12
  • 60
  • 109
0
votes
0 answers

Open CV - Is there is a way dispatch a method, by the underline pixel type?

I open an image using imread(), getting a cv::Mat instance. I wish to apply a function on this matrix, by its underline pixel type. I wish to know if there is a Dispatch() function in openCV? Here is a code sample. FindFeature(cv::Mat_&…
ShaulF
  • 841
  • 10
  • 17
0
votes
1 answer

Implement abstract behaviour just once... trait as contract, abstract class as concrete-helper

I'm currently thinking about refactoring my personal linear-algebra package. One thing that really bothers me is: Currently I only support Vectors and Matrices that consist of floats. Now I'd like to add in support for ints, doubles and possibly…
user4063815
0
votes
1 answer

How to use a Type variable as a Type Parameter in a loop?

I want to do this: foreach(Type t in Aplicables) { filename = Path.Combine(dataDir, t.Name + "s.txt"); tempJson = JsonConvert.SerializeObject(DataRef.DataList.ToArray()); System.IO.File.WriteAllText(filename, tempJson); } But I…
0
votes
1 answer

Rationale behind member function swap

In the standard library, if a class type has a specialized swap algorithm, then it will have a member function swap and a free function swap that simply forwards to the member function. I don't quite get the rationale behind having both of them (and…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
0
votes
0 answers

How to make a Generic library closed source?

Why cannot a C++ library with Generics (ie. templates) be closed source ? Suppose I want to make an object file for my library and provide only the implementation and the header file, what should i do ? I understand that at the time of linking the…
Rohith R
  • 1,309
  • 2
  • 17
  • 36
0
votes
1 answer

Example of Generic Delegate?

Generic classes can declared with generic delegates as shown below but this is unfortunatelly metadata. I can not find full example: public abstract class Expression { public static Expression Lambda(Expression body, bool…
0
votes
1 answer

how to reparameterize collection type

I want to define a transformation of a generic collection. def transform[S<:GenIterable[T],T](s:S):S = s.zipWithIndex This throws: type mismatch; found : scala.collection.GenIterable[(T, Int)] required: S I have to declare S as a parameterized…
0
votes
1 answer

Undefined reference to static member variable when specializing

I have the following code: struct All { All() {} ~All() {} template static struct Item { T var; } item; virtual void setVal() noexcept {} }; template struct any : public All { …
0
votes
1 answer

Pipeline class storing different templates with constraints

I have a class template DataProcessor, which looks like this: struct DataProcessorBase { typedef std::shared_ptr Ptr; }; // struct DataProcessorBase template struct DataProcessor :…
Moos Hueting
  • 650
  • 4
  • 14
0
votes
1 answer

generic programming with polymorphism and generic vector

I have this code: struct All { public: All() {} ~All() {} }; template struct any : public All { public: any() : All() {} ~any() {} T value; }; int main() { any* a = new any; a->value =…
0
votes
4 answers

Difference between and <>

I have noticed that below code is completely legal in netbeans: HashSet hashSet = new HashSet<>(); However eclipse is not happy with that, and I have to initialize it like this: HashSet hashSet = new HashSet(); //…
Mustafa Shujaie
  • 1,447
  • 1
  • 16
  • 30
0
votes
0 answers

Accessing parent class methods of the parameter of a generic class, through object of generic class

I am working on project. I need to Access parent class methods of the parameter of a generic class. The structure is like: public interface XYZ{ methodToAccess1(); } public interface PQR{ methodToAccess2(); } An instance through which I have…
0
votes
2 answers

Java generic type restricting class type

I am writing a client wrapper for a service. Which have given client implementation. Having Two class hierarchy: Account Product Both classes doesn't share common parent interface. But they while using them they have similar…
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186