gr1ph00n
gr1ph00n

Reputation: 65

Java generics: parametricise a generic type

Good Morning, I am working on some java code and i would like to make something like that:

public class MyClass <E, T extends MyInterface<T<E>>> { } 

In this way, T should be both a generic and an instance of a class which implements MyInterface and should be parametric of the type E. But it seems it is not possible in Java, or maybe my approach to the problem is wrong. Thanks in advance.

Update:

First of all thanks to all who replied, now just to make things clear and let You understand what I am trying to achieve, I would like to make a generic container of generic TDAs. So let's say we need a container of Stacks based on Integers, then I would need to write something like that: MyContainer, of course i need to keep types separated for the insertion part, otherwise i could write MyContainer>. Moreover since all my TDAs implements MyInterface, I would like to set this as bound as well. So i guess

public class MyContainer < E, T extends MyInterface < E>> 
should do the trick.

Upvotes: 0

Views: 140

Answers (4)

John B
John B

Reputation: 32969

Seems like it should be

public class MyClass <E, T extends MyInterface<E>> { } 

Having public class MyClass <E, T extends MyInterface<T<E>>> { }

means that T

should be MyInterface<MyInterface<E>> which doesn't seem likely.

EDIT:

So if you need MyInterface<Class1<Class2>> it seems like it should be something like this:

public class MyClass <A, B extends SomeClass<A>, C extends MyInterface<B> { }

or

public class MyClass <E, T extends MyInterface<SomeOtherClass<E>>> { }

Upvotes: 0

Piotr Praszmo
Piotr Praszmo

Reputation: 18330

Basically you are trying to do something like this:

class MyClass<T<E>> // T has to be a generic class

or this:

class MyClass<T<Integer>> // T has to be a generic with Integer as parameter

You cannot and shouldnt have to do it, because it's meangingless. It doesn't tell you anything usefull about T. You could use any class as T like this:

DummyClass<T> extends Anything {};
MyClass<DummyClass<Integer>> myClass;

Upvotes: 1

Jon
Jon

Reputation: 3602

This doesn't make sense.

public class MyClass <E, T extends MyInterface<T<E>>> { } 

Because of the right-most T. Why would you want to parameterise a class with itself? I think what you really mean is this:

public MyClass<E, T extends MyInterface<E>>

Which essentially means that for every MyClass, you need an E, and a T MyInterface instance which is in turn parameterised to work with E.

I've found beginners can get very carried away with generics, which can result in code which is completely unreadable. Use generics sparingly, and think about whether you really need them in each given instance. If you are finding yourself writing code with multiple levels of nested generics parameterisation, then you're probably doing something wrong in terms of design.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691913

I'm not sure if that's what you want, butI think it could just be

MyClass<E, T extends MyInterface<E>>

T<E> doesn't make much sense to me.

Upvotes: 0

Related Questions