Questions tagged [generics]
Meta technique, that allows to pospone the setting of the dependable type to the runtime.
155
questions
8
votes
4
answers
2k
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 ...
2
votes
3
answers
22k
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
2
answers
1k
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
1
answer
763
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
1
answer
986
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
1
answer
2k
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
1
answer
147
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
1
answer
781
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
8
answers
7k
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 ...
3
votes
0
answers
72
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
1
answer
123
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
2
answers
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 ...
0
votes
2
answers
1k
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
2
answers
5k
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
1
answer
289
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
2
answers
2k
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
1
answer
878
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 ...
8
votes
2
answers
7k
views
Why generic interface cannot implement dynamic type?
If it possible
IList <dynamic> = new List <dynamic>;
or
class A <T> { }
class B: A <dynamic> { }
Why it is not possible to do
class U: IEnumerable <dynamic> {}
?
36
votes
12
answers
8k
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
2
answers
625
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 ...
9
votes
1
answer
18k
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
2
answers
662
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
1
answer
519
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) {
...
8
votes
2
answers
359
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
1
answer
344
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
4
answers
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 ...
13
votes
1
answer
4k
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
1
answer
246
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 ...
5
votes
0
answers
3k
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 ...
26
votes
1
answer
27k
views
What is the difference between <? extends Foo> and <Foo>
I seem to have a misunderstanding about the difference between <Foo> and <? extends Foo>. From my understanding, if we had
ArrayList<Foo> foos = new ArrayList<>();
This ...
28
votes
3
answers
26k
views
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 ...
6
votes
1
answer
532
views
What is the most generic way to provide a variable amount of outputs from a Rust function?
I am currently writing an API for machine learning algorithms in Rust and I would like for a single genetic algorithm, artificial neural network, or Bayesian network to provide multiple outputs so ...
4
votes
2
answers
596
views
Exposing nodes in a generic binary search tree implementation to visitor pattern
It's common to implement binary search trees with an internal Node class
public class BinarySearchTree<TKey, TVal>
{
class Node
{
public readonly TKey Key;
public ...
7
votes
1
answer
5k
views
Why is there not a basic SortedList<T>?
As far as I know, there is no way to get a List<T> with the capacity to sort itself. If I add a element, I want the data-structure to insert it in the right position (based on the associated ...
0
votes
3
answers
2k
views
Multi inheritance in Java
Let's say I've got a generic java class Filter<InputType, OutputType> which receives an input object and transforms it to an output object.
Now I've got two other classes (NoInputFilter<...
12
votes
7
answers
10k
views
When to use generics in interface design
I have some interfaces that I intend third-parties to implement in the future, and I provide a base implementation myself. I'll only be using a couple to show the example.
Currently, they are defined ...
1
vote
3
answers
6k
views
Advantage of generic base class [closed]
What's the advantage of having a generic base class and a derived class having itself as the generic parameter on the base class.
e.g.
public abstract class MyBase<T> {}
public MyDerived : ...
4
votes
1
answer
2k
views
Lambda Return Type Inference
Writing my own JVM compiler, I am facing a giant problem that I am desperately unable to solve:
Lambda Return Type Inference
1. Overview of the compiler lifecycle
More specifically, the order in ...
1
vote
1
answer
175
views
Should I use generics whenever possible or is object ok in some cases?
I'm having an interface ProgressReporter which basically takes a list of input items (the working queue) through a method SetWorkPackages(IEnumerable<T> workPackages) and then has another method ...
2
votes
2
answers
437
views
Design pattern for ensuring the "signature" of generic lists is equal
I have an interface, Attribute, that is going to be implemented in various ways,
class AttributeA implements Attribute{\\..}
class AttributeB implements Attribute{\\..}
Then I'm planning to have a ...
1
vote
2
answers
3k
views
Generics in C# Test Class
I'm learning about unit testing in C#. Languages_Service and PlaceOfWork_Service are classes for SOAP services which Microsoft Navision generated for me. There are several methods that are very ...
17
votes
2
answers
2k
views
Is higher-rank parametric polymorphism useful?
I'm pretty sure everyone is familiar with generic methods of the form:
T DoSomething<T>(T item)
This function is also called parametrically polymorphic (PP), specifically rank-1 PP.
Let's ...
15
votes
1
answer
3k
views
Why do you need higher kinds?
Some languages allow for classes and functions with type parameters (such as List<T> where T may be an arbitrary type). For example, you can have a function like:
List<S> Function<S, T&...
16
votes
3
answers
6k
views
What is the reason of using an interface versus a generically constrained type
In object-oriented languages that support generic type parameters (also known as class templates, and parametric polymorphism, though of course each name carries different connotations), it is often ...
3
votes
1
answer
205
views
How to design similar purpose static methods
I know that you cannot override static functions, but for what I'm looking to do, it seems like the logical way to design my class.
I have a method that is associated with my abstract class, that i ...
4
votes
2
answers
3k
views
Do wildcards in Java generics restrict or increase flexibility?
I have read many sources about wildcards and Java generics. Even though I have seen many explanations and answers, none of them seems correct.
The question is very simple: Do wilcards in Java ...
2
votes
2
answers
2k
views
Is using generics to indicate some class/method is aware of a type good idea?
I'm going to use Java for code/idea demonstration.
Imagine it's needed to define two interfaces: Observer and Observable (observer pattern). It's a good idea to have two generic interfaces, so there ...
5
votes
1
answer
1k
views
Using macros to implement a generic vector (dynamic array) in C. Is this a good idea?
So far I have only done personal projects at home. I hope to get involved in some open source project some time next year. The languages I that have been using the most are C and C++. I have used both ...
5
votes
1
answer
3k
views
OK to make a method generic just to pass in a System.Type?
Sometimes I have a function that takes a System.Type parameter, like:
public void Foo(Type type) {
// do something with "type"
...
}
This must be called like this:
Foo(typeof(MyType));
I ...
5
votes
1
answer
2k
views
Extend, wrap, or both to add generics to a class that should have had them?
So I'm using a C# framework that has a great example of where generics would be useful, except they weren't used. For simplicity's sake, we will say it was a list (I know C# has its own list), but ...