Skip to main content

Questions tagged [generics]

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

Filter by
Sorted by
Tagged with
1 vote
4 answers
210 views

Due to type erasure, I can't do instanceof T in Java. With that in mind, how do (should) I write, for example, a generic TreeNode (or, more precisely, DefaultMutableTreeNode)? setUserObject() accepts ...
Sergey Zolotarev's user avatar
6 votes
3 answers
1k views

There is a blog post by Eric Lippert where he describes how to define variance. In a general sense, covariance is achieved when the direction of assignment compatibility is preserved. Contravariance ...
Ramza's user avatar
  • 171
0 votes
1 answer
357 views

I have a class where I want to chain certain operations. The class look something like this class MyClass<T> { create<X>(fn: (_: T) => X): MyClass<X> { ... ...
pratZ's user avatar
  • 119
-2 votes
3 answers
856 views

Refer to the highest upvoted answer here for why you should make an IPizza interface with a IPizza.Prepare() method. This is highly upvoted, But I think the answer is flawed. Sure a pizza can be ...
NWoodsman's user avatar
  • 195
1 vote
1 answer
689 views

I'm trying to create an interaction system for a game I'm developing with Unity and C# and I've been struggling with it for a while now. It consists of interactors and interactables. The idea is that ...
Wojtek Wencel's user avatar
3 votes
2 answers
1k views

I've recently found myself using a generics with constraint that a type should inherit from a specific base class, but now I've just realised that this is redundant and unnecessary because the ...
Piotr Golacki's user avatar
0 votes
1 answer
138 views

I'm currently designing an interface for a container that is supposed to store references of different instances that derived from a common supertype. An analogy of it would be as following: Suppose ...
torez233's user avatar
  • 101
1 vote
1 answer
486 views

I currently working on a parser project in C# and have run into problem. I have an entity folder within my project and within it I have: Entity IEntity.cs (defines a contract for entity classes) ...
Vocaloidas's user avatar
0 votes
1 answer
5k views

Abstract The case: Application contains a lot of views with a list of data. Data views (lists of records) have pagination, filtering and sorting options. The user must be able to select a "...
user2190492's user avatar
4 votes
2 answers
9k views

I've written a class that synchronizes a table between two databases using Dapper. The public and private Methods in the class are generic and the generic parameter is the POCO class that is being ...
GisMofx's user avatar
  • 379
0 votes
0 answers
102 views

I have 2 classes that share some properties public class SportsUser { public string errorCode { get; set; } //the sportsuser and SportsAdminUser is from a different database hence the long and ...
pinman's user avatar
  • 11
6 votes
2 answers
1k views

I wrote this valid piece code, which made me wonder if there was a name for it: public class GenericObject<T> { public T Obj { get; set; } } public class DerivedClass: GenericObject<...
Taco's user avatar
  • 1,175
18 votes
7 answers
8k views

YAGNI might tell us, that in the below implementation the generic version is not needed, as long as the function is only used once. But to me personally, it seems, the generic version is more readable ...
Tobias Hermann's user avatar
1 vote
1 answer
125 views

Here is a concrete example of what I'm asking about: MutableList[ImmutableList[Object]] Where ImmutableList is covariant wrt its first parameter, but MutableList is invariant wrt its first parameter. ...
Beks_Omega's user avatar
1 vote
0 answers
456 views

I can easily put together a message handling set of generic apis in C# with the following quick and dirty code: void Main() { var messageClient = new MessageClient(new TestMessageHandler(), new ...
Andez's user avatar
  • 259
0 votes
1 answer
218 views

I am in the process of implementing a persistent collection in C, specifically, an immutable hash trie. In order to increase acceptance and reusability, I have identified the following key areas that ...
ammut's user avatar
  • 159
-1 votes
1 answer
123 views

I have a problem in my app where I have many entities that can all reference each other in different ways. For example, I have a Job (e.g. build house) that I might assign to a team called "Plumbers" ...
Carson Holzheimer's user avatar
1 vote
0 answers
154 views

I am making an SNMP agent. In order to pass information to this SNMP agent, I need to periodically extract data from two different sources (and there may be more sources in future.) I am trying to ...
Navjot Singh's user avatar
1 vote
1 answer
372 views

Recently I have asked this question: How do you rewrite the code which using generics and functionals in Java 8 and mixing oop and functional programming by using only object-oriented? on ...
Taha Yavuz Bodur's user avatar
-2 votes
1 answer
278 views

I am trying design a generic hardware facade interface for different camera types. below is my incomplete generic interface for camera devices template <class Data,class Configuration> class ...
eliranno's user avatar
1 vote
2 answers
215 views

I have Handler classes which accepts Queries and returns Results. Handlers is anemic. They accept input data bag and returns output data bag. Handlers can be many so I created common generic interface ...
user avatar
1 vote
0 answers
145 views

I have a class that could: Have multiple types of containers Have multiple types of implementations and what I did to model so far is: public interface ChildClass { Container getContainer(); ...
Hasan Can Saral's user avatar
2 votes
3 answers
3k views

When I was looking this question, a question comes my mind. Think about using an interface like : public interface ICommandProcessor<T> where T : ICommand { void Process(T command); } When ...
Engineert's user avatar
  • 929
1 vote
2 answers
535 views

Given this generic class and constructor, class A<E extends Number> { A(E number, Comparable<E> comparable) { //... } } it is ensured that a call to the generic ...
user1803551's user avatar
0 votes
1 answer
69 views

I am implementing a Binary Search Tree and cannot decide if it would be good practice to constrain the generic type of the tree to a comparable item, i.e. IComparable<T>. Or, to just use the ...
Connor Blakey's user avatar
0 votes
1 answer
2k views

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 ...
user avatar
0 votes
0 answers
2k views

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 ...
Jallrich's user avatar
  • 119
1 vote
1 answer
245 views

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 ...
Marco's user avatar
  • 337
5 votes
4 answers
19k views

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 ...
Rowan Freeman's user avatar
0 votes
2 answers
266 views

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 ...
Garikai's user avatar
  • 173
0 votes
1 answer
233 views

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 ...
dubbeat's user avatar
  • 109
-2 votes
1 answer
239 views

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 ...
Nader's user avatar
  • 119
0 votes
1 answer
151 views

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 ...
Adam B's user avatar
  • 1,660
3 votes
1 answer
6k views

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 ...
radian's user avatar
  • 103
0 votes
1 answer
518 views

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 ...
Jordane's user avatar
0 votes
1 answer
220 views

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 ...
Vince Orio's user avatar
0 votes
1 answer
2k views

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 ...
user avatar
0 votes
2 answers
274 views

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&...
ryano.mcc13's user avatar
1 vote
3 answers
548 views

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 ...
Evan Aad's user avatar
  • 121
9 votes
2 answers
684 views

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 ...
Levi Morrison's user avatar
-1 votes
2 answers
4k views

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 ...
Maxracer's user avatar
1 vote
1 answer
197 views

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. ...
Zohar Peled's user avatar
37 votes
4 answers
12k views

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 ...
SyntaxRules's user avatar
3 votes
1 answer
621 views

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."...
SaganRitual's user avatar
4 votes
1 answer
5k views

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 ...
Mahesha999's user avatar
3 votes
2 answers
1k views

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 ...
Chris Halcrow's user avatar
0 votes
2 answers
2k views

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 ...
Lock's user avatar
  • 379
1 vote
2 answers
179 views

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 ...
AnotherDeveloper's user avatar
1 vote
2 answers
431 views

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 ...
Yeldar Kurmangaliyev's user avatar
14 votes
2 answers
6k views

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 ...
John Wu's user avatar
  • 27k