Questions tagged [generics]

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

It is computer programming technique that allows a method/function/class to be defined, depending not only on concrete data types, but on some undefined types also. These types will be chosen concretely upon instantiation

155 questions
68
votes
1 answer

Java: "Heap pollution"

A "Heap Pollution" as in Non-Reifiable Types (The Java™ Tutorials > Learning the Java Language > Generics (Updated)) Why is it called that way?
user18404
54
votes
7 answers

What is wrong with Java's generics?

I have seen several times on this site posts that decry Java's implementation of generics. Now, I can honestly say that I have not had any issues with using them. However, I have not attempted to make a generic class myself. So, what are your issues…
Michael K
  • 15,539
  • 9
  • 61
  • 93
47
votes
6 answers

Good or bad practice to mask Java collections with meaningful class names?

Lately I've been in the habit of "masking" Java collections with human-friendly class names. Some simple examples: // Facade class that makes code more readable and understandable. public class WidgetCache extends Map { } Or: // If…
herpylderp
  • 2,017
  • 3
  • 21
  • 27
37
votes
4 answers

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 someProperty =…
SyntaxRules
  • 581
  • 1
  • 4
  • 13
36
votes
12 answers

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{ public: Sword(){ this->name="Sword"; } …
ggrr
  • 5,725
  • 11
  • 35
  • 37
29
votes
3 answers

A good generic type system

It's commonly accepted that Java generics failed in some important ways. The combination of wildcards and bounds led to some seriously unreadable code. However, when I look at other languages, I really can't seem to find a generic type system that…
Peter
  • 469
  • 4
  • 9
28
votes
8 answers

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 int IntProperty { get; set; } } These classes…
Vladimir Stokic
  • 2,943
  • 14
  • 25
27
votes
3 answers

Generics vs common interface?

I don't remember when I wrote generic class last time. Every time I think I need it after some thinking I make a conclusion I don't. The second answer to this question made me to ask for clarification (since i can't comment yet, i made a new…
jungle_mole
  • 381
  • 1
  • 3
  • 11
26
votes
1 answer

What is the difference between and

I seem to have a misunderstanding about the difference between and . From my understanding, if we had ArrayList foos = new ArrayList<>(); This indicates that objects of type Foo can be added to this array list. Since…
Zymus
  • 2,403
  • 3
  • 19
  • 35
21
votes
5 answers

What's a good naming convention for generic types in C#?

I decided to ask this question here instead of on stack overflow because it is rather subjective. In C#, typically I see generic types with very poor names. Specifically, "T" is commonly used but is not a meaningful name by itself. For…
void.pointer
  • 4,983
  • 8
  • 30
  • 40
21
votes
2 answers

Why is there a new() constraint in C# but no other similar constraint?

In C# generics, we can declare a constraint for a type parameter T to have a default constructor, by saying where T : new(). However, no other kinds of constraints like this are valid - new(string) for example, etc. From a language design and/or…
20
votes
4 answers

Java - Use polymorphism or bounded type parameters

Suppose I have this class hierarchy... public abstract class Animal { public abstract void eat(); public abstract void talk(); } class Dog extends Animal { @Override public void eat() { } @Override public void talk() { …
HugoMelo
  • 325
  • 1
  • 3
  • 6
20
votes
4 answers

Who extends interfaces? And why?

AFAIK, my class extends parent classes and implements interfaces. But I run across a situation, where I can't use implements SomeInterface. It is the declaration of a generic types. For example: public interface CallsForGrow {...} public class…
Gangnus
  • 2,805
  • 4
  • 21
  • 31
20
votes
9 answers

How to spread awareness for generic programming among team members?

I am staying in an environment, where people believe: Java generics are the feature exclusively used for library writing and not for the real coding. C++ is an OO programming language; template is an optional and avoidable feature Though, these…
iammilind
  • 2,232
  • 4
  • 24
  • 35
18
votes
7 answers

Should one prefer a generic version of a function, even if it's not re-used (yet)?

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 because I'm not distracted by all the…
Tobias Hermann
  • 608
  • 5
  • 14
1
2 3
10 11