Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [generics]

Meta technique, that allows to pospone the setting of the dependable type to the runtime.

0
votes
1answer
55 views

How to define a structure to store some generic “Variable” classes for easy access later

I'm running into a design problem. My code is in C# but the concepts could apply to any OO language. I'm designing a framework to run experiments, and these experiments have several variables which ...
1
vote
1answer
69 views

Using generics on interfaces when implementation is not generic

Is it acceptable practice to put generics on an interface when the implementation will not be generic? Lets say that my project will have many classes that read data from the database. I may make a ...
-1
votes
1answer
90 views

Downcastings refactored using generics

I am willing to refactor some code that acts like a controller class executing work embedded in other classes. On one side it looks good as the controller is generic and what changes has been well ...
0
votes
0answers
41 views

I need to do pagination for soap calls. What patterns would fit?

Core reason: Mainframe developers exposed search soap-services for different entities. Those soap services, have inconsitent indicator for pagination, xml tags are different, so that would require ...
-1
votes
1answer
180 views

Is there a design for C# that removes the need for dynamic?

I have a web service that implements Repository Pattern and I was wondering if there is a design in C# that can remove the need for 'dynamic' when injecting it in my method. public interface ...
0
votes
1answer
141 views

Should I avoid nested generic collections?

I recently came across a dictionary with lists for values passed as an argument: Demo(Dictionary<string, List<string>> arg) It got me thinking---normally I would abstract any list or ...
0
votes
2answers
125 views

Pattern name for a class inheriting of a generic based on itself

I'm wondering if there is a name for the pattern of defining a generic class with one type argument where the type inherits the defining class. For example public class A<T> where T : A<T&...
0
votes
1answer
170 views

Is there a programming language with objects and generics, but without inheritance?

The GO programming language has objects but no inheritance. It also has generics for the built-in types Array and Map. Is there a programming language with objects but no inheritance and with generics ...
9
votes
1answer
103 views

Name of technique for inferring type arguments of a type parameter?

Setup: Let's assume we have a type called Iterator which has a type parameter Element: interface Iterator<Element> {} Then we have an interface Iterable which has one method which will return ...
-1
votes
1answer
795 views

C#, Static classes and Inheritance

As a beginner I'll try to explain my problem as good as I can: I'm currently trying to program a "simple" ECS. My basic idea is that I have a base "Entity class" which includes all sorts of functions ...
0
votes
0answers
69 views

Add and remove interfaces

I was writing code the other day in Java, and was suddenly struck by a desire for the ability to add and remove interfaces from generics. For instance, something like: public <T extends A, IX> ...
0
votes
1answer
114 views

Is this still considered an implementation of the strategy pattern?

In my project I have different types of entities. I get the data for these entities in text files from a 3rd party. I've written a class to read and parse these text files, using the strategy pattern. ...
36
votes
4answers
7k views

What is generics abuse?

While reviewing some code, I noticed the opportunity to change it to use generics. The (obfuscated) code looks like: public void DoAllTheThings(Type typeOfTarget, object[] possibleTargets) { var ...
1
vote
1answer
256 views

Why doesn't Swift's Numeric protocol inherit from the Comparable protocol?

I created a generic class MyClass<T: Numeric> {...} and got errors in my functions that tried to use > and <, along the lines of "Binary operator '>' cannot be applied to two 'T' operands."...
4
votes
1answer
1k views

Designing generic type inheritance hierarchy

I have now put another rephrased version of original question as requested by the user in the comments with class names mimicking real world scenario of postal office (though I dont know how real ...
3
votes
2answers
300 views

What is the benefit of using an interface that doesn't enforce anything (marker interface)?

The codebase I'm working on makes a lot of use of interfaces that don't actually enforce anything. They're in place more to make sure that a type 'is a' something. For example: public class ...
-1
votes
2answers
86 views

How to create a generic client to query an API and return different types based on API call?

I am building a repository that will request data from an API. Originally, I had multiple methods in my repository such as GetOrders(), GetOrderItems() etc. The majority of these functions had almost ...
3
votes
2answers
88 views

Scan Relationships of Entity Framework Entity Generically

I have an application with a database that doesn't allow cascading deletes. When a user goes to delete an entity, I would like to display a popup saying something generic like, "This record has ...
1
vote
2answers
247 views

How to combine these fill and create methods or utilize generics in factory?

Domain object "Contragent" Let's say I have an hierarchy of classes: public class BaseContragent { public int Id { get; set; } } public class PersonContragent : BaseContragent { public string ...
9
votes
2answers
3k views

Why use a generic method with a type constraint instead of the type itself?

In a different StackExchange question, I noticed someone using this prototype: void DoSomething<T>(T arg) where T: SomeSpecificReferenceType { //Code.... } Bearing in mind there is only a ...
1
vote
5answers
340 views

How to avoid object cast after passing a parameter?

I have a problem with implementing generic user interface interaction. I have different classes that contain data each for particular interface element. So every UserInterfaceElementComponent has ...
6
votes
4answers
1k views

How to ensure a method handling each event in an event system?

I have several types of events, for example: abstract class Event { static class KeyPress extends Event { ... } static class KeyRelease extends Event { ... } static class KeyHold extends ...
1
vote
3answers
5k views

Converting Method to Return Generic Type

I would like to ask how to convert a method so that it can return any type depending on what happens within it. What I have is the start of an application which will visit a number of webpages (and ...
1
vote
2answers
566 views

Arbitrary protobuf message as byte-array over Websocket - how to determine actual message type upfront

Protobuf is nice and dandy, but it was not made with self-description in mind. Now this is totally okay if you're using a well-defined protocol and want to replace e.g. SOAP-messages or just want to ...
1
vote
1answer
393 views

How to figure out what interface to cast to?

This question may be a bit subjective, but I have tried three different solutions and none of them has felt right. I will provide some context and the solutions I have tried. The issue I am facing ...
3
votes
1answer
520 views

C++ generic graph search algorithm with generic data types

I'm trying to implement a generic graph search algorithm in C++, as part of assignment at university, and I encountered problems when trying to implement it, mostly struggling with templates. this is ...
4
votes
1answer
484 views

When using generics for both in and out types, why is out type generally last?

This is a bit of a subjective question I guess but I was hoping for opinions on either side of the argument. So take this for example in C#: public interface IFactory<Tin, Tout> : IFactory { ...
4
votes
1answer
111 views

What alternatives are there to using an object of a contravariant interface polymorphously?

The gist of what I'm trying to do is get an instance of the appropriate user service, then pass it whatever subtype of User we're working with. Models: public class User { public string ...
4
votes
1answer
239 views

Are there only 2 places you can declare new generic type parameters in Java?

It's confusing because type declaration and usage both use the <T> syntax. I think there are only 2 places where you can declare new generic types in Java: 1. In the definition of a class or ...
27
votes
8answers
5k views

C# Generics - How to avoid redundant method?

Let's assume I have two classes that look like this (the first block of code and the general problem are related to C#): class A { public int IntProperty { get; set; } } class B { public ...
1
vote
0answers
56 views

Entity Framework - Old school save/update advice?

I've a query for you relating to best use of EF6 when using Winforms yet not having justification for changing things to something more modern. I've got half a dozen classes that are based on ...
4
votes
1answer
89 views

The bound mechanism by generics for a type variable to appear in its own bound

From Programming Languages: Principles and Paradigms By Maurizio Gabbrielli, Simone Martini The bound mechanism for type variables is fairly sophisticated and flexible. In particular, a type ...
2
votes
2answers
2k views

Java Generics, difference between two methods signatures with wildcard vs type parameter

I'm having a hard time trying to figure out why the two methods declarations have different results. On the first method signature its obvious you can't compare Apples with Apples and Oranges with ...
-1
votes
1answer
209 views

template restrictions in C++

I am creating a template class in C++ for numeric operations and I want to restrict it to only numbers or similar numeric types. My question is that is there any way in C++ to restrict a template ...
1
vote
2answers
2k views

Example of problem caused by casting Object[] to E[]

I've heard here and there that arrays contain runtime data about their type, making you unable to instantiate an E[] (E being a generic type parameter, for example in a class Foo<E>), and that ...
-2
votes
1answer
231 views

What is an efficient way to implement 'Stack' without generics?

Today I was reading a lot of articles complaining about Java Generics or C++ Templates. Now, my question is: How can someone implement a collection data structure like Stack without using them?
0
votes
2answers
1k views

Java - Does extending a class which itself extends a class which itself… and so on… is healthy..?

NOTE : Feel free to edit the title if it is somewhat different than my question. In one of our application, we are maintaining a lot of properties inside Instance object. Obviously it's maintained as ...
8
votes
1answer
254 views

How to resolve generic typenames in compiler?

Let's say I have such code (and its meaning is like in C#): class Foo<T> { public T my_field; } and later in code I have: var foo = new Foo<int>(); foo.my_field = 5; My problem ...
7
votes
2answers
3k views

Why generic interface cannot implement dynamic type?

If it possible: IList <dynamic> = new List <dynamic>; or: class A <T> { A(T){} } class B: A <dynamic> {} . Why it is not possible to do this: class U: IEnumerable <...
33
votes
12answers
6k views

Is it a code smell to store generic objects in a container and then get object and downcast the objects from container?

For example, I have a game, which has some tools to increase the ability of the Player: Tool.h class Tool{ public: std::string name; }; And some tools: Sword.h class Sword : public Tool{ ...
8
votes
2answers
420 views

Generic request processing

I got a webservice that accepts multiple calls that require different handling and validation, using generics I've managed to create a common class that accepts a handler and a validator and it looks ...
6
votes
1answer
4k views

Why Generic type can not instantiated?

Here is my question. class Gen<T> { T ob; Gen() { ob = new T(); // Illegal!!! } } Why is it illegal? Could you please explain it.
0
votes
2answers
343 views

Creating a Vector (physics) Class in Java

I'm trying to create a vector (like in physics) class in Java. I want to support vector addition, dot product, scalar multiplication, etc. So, my question is, is it possible to do this with generics? ...
1
vote
1answer
238 views

What's the benefit of generics in an in interface e.g. IEnumerable<HttpPostedFileBase>

I've come across a method that requires a parameter that implements IEnumerable and is of a certain type like this: public myMethod (IEnumerable<HttpPostedFileBase> myParameter) { ...
6
votes
1answer
186 views

Should an interface covariant on T ever implement an interface invariant on it?

I was reading through the Hacklang docs on Collections, and came across this curious definition [paraphrased slightly]: interface KeyedIterable<Tk, Tv> ... interface ConstVector<+Tv> ...
3
votes
1answer
304 views

Bounded Type Parameters

Ref: https://docs.oracle.com/javase/tutorial/java/generics/bounded.html, We have the method: public <U extends Number> void inspect(U u) Why can't this be: public void inspect(Number n) ? It ...
13
votes
4answers
6k views

If my IDE is so smart, why do I need to cast “clone()”?

My IDE (NetBeans) type checks my Collections while I am typing code. But then, why do I have to cast the returned object of Object.clone()? Which is fine. No harm no foul. But still, I don't ...
7
votes
1answer
2k views

Why covariance is forbidden for abstract class in C#

I've been digging in covariance and contravariance in C# and there is one thing I could not manage to understand. C# (AFAIK, as of 4 version) allows one to declare the covariant or contravariant ...
3
votes
1answer
209 views

How does C# remove duplicate instances of static data of constructed types?

Unlike Java, C# does not share static data among entire generic type, but per each constructed type (i.e. Array<int>, Array<string> and so on). Creating those instances does not seem a ...
1
vote
0answers
1k views

Generic Type Parameters where one is itself Generic with a Type Parameter of the Other

I have a couple of classes with the following pattern that I am uncomfortable with. Is this the only way to do this, or are their other design patterns that would be more reliable. public class ...