Can I require a generic parameter to have generic parameters?

Viewed 54

I would like to write a class that looks like this:

public class SomeClass<T<?>> {
    T<String> someField;
    // ...
}

But this is not valid Java. Is there any way to tell Java that the generic parameter T must be a class that accepts one generic parameter, such as List, ArrayList, Supplier or Consumer.

2 Answers

In a word, no, there isn't. You could limit T to extend some class or implement some interface, but you can't limit it to being a generic class itself.

There are no higher kinded types in Java. You'll need Scala (or Haskell or Rust) for that sort of thing.

Related