Questions tagged [generics]

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

0
votes
1answer
139 views

Do I need to unit test a generic method with all accepted types?

For a method whose signature looks like this: public T Add<T>(T first, T second) where T : struct, IEquatable<T>, IComparable<T> which can work with all of the integral types, do I ...
0
votes
0answers
65 views

Inherit a class that inherits a generic class to make it easier to read

I'm creating a simple Dependency Injection library for Unity (no constructors available) and I want to use a generic class that implements a generic call for each class that would inherit it allowing ...
1
vote
1answer
108 views

Why doesn't Comparable<T> include any type bound? [closed]

Why do you think the definition of Comparable<T> lacks an upper bound on T? That is, why is it not defined as: Comparable<T extends Comparable<?>> or Comparable<T extends ...
0
votes
4answers
531 views

Is it possible to infer the the generic type from a generic method?

I'm making an HTTP API client class. I want a Perform() method which takes a request object and returns an obejct that is expected from the API. So a PostRequest object will describe how to get a ...
0
votes
2answers
214 views

How best to avoid member implementation of class' Abstract/Interface instance variables

I'm sorry if the phrasing of the question is a bit unclear but let me try to clarify below. (If anyone can word it better, feel free to edit) I have a Map instance variable, groups, which is defined ...
0
votes
1answer
90 views

What design pattern would help me make my factory more generic

In my code I have 2 separate login types. I have a factory that decides which one to create based on an enum. Each login type has a different type of credential. Currently my factory method takes ...
-2
votes
1answer
154 views

generate code or write generic code [closed]

I am writing drivers for different devices to work with my embedded system. Currently I write any new driver manually but for the future I would like to automate this using a settings file. I figure I ...
0
votes
1answer
62 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
628 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 ...
0
votes
1answer
96 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
1answer
183 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
299 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
128 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
3answers
328 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
187 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 ...
0
votes
2answers
1k 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
1answer
124 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
8k 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
346 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
2k 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
372 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
110 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 ...
2
votes
2answers
103 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
314 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 ...
13
votes
2answers
5k 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
384 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
8k 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
726 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
558 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
618 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
750 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
117 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 ...
3
votes
1answer
375 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 ...
28
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 ...
2
votes
0answers
58 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
101 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
3k 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
225 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
235 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 ...
7
votes
1answer
311 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
5k 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
7k 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{ ...
7
votes
2answers
431 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
6k 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
406 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
264 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
195 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> ...