Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic type argument in java generics

Is there a way that I can pass a generic type to a generic class like

interface Wrapper<T, R> {}

interface Result<T, Wrapper<T, R>> {
    R getResult();
}

Here Result takes Wrapper one of the type with type arguments T and R. getResult method in Result interface returns R.

What I did is below,

interface Result<T, W> {
    <K> K getResult();
}

Is there a better way to do this in java.

like image 439
Praveen Avatar asked Nov 17 '25 11:11

Praveen


1 Answers

You need a separate type argument for a Result interface. Something like this:

interface Result<T, R, W extends Wrapper<T, R>> {
    R getResult();
}

Now you can use W inside the result to represent the Wrapper or T and R to represent wrapper components. It's possible though that W is completely unnecessary for you and having T and R is enough.

like image 175
Tagir Valeev Avatar answered Nov 19 '25 00:11

Tagir Valeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!