R.L
R.L

Reputation: 109

Is there a way to reduce multiple type parameters?

I have a very generic heavy class where i am testing differents approach to a problem and this class is used in a lot different places and everytime i change the number of type parameters i have to fix errors everywhere i use the type even where i dont use that class genericity

public class HeavyGen<TYPE1, TYPE2, TYPE3, ETC>{...}

//now anywhere else i have to do either this and refactor my code at each 
//change in the number of type parameters

public HeavyGen<?,?,?,?> func(){...}

//or this but @SupressWarning seem like a dirty fix

@SupressWarning
public HeavyGen func(){...}

So: is there a way to be able to change the number of type parameter of HeavyGen while not having to refactor code in places that dont use it genericity?

Ideas I had:

Upvotes: 3

Views: 794

Answers (1)

IlyaMuravjov
IlyaMuravjov

Reputation: 2492

You can simply rename your Container<...> to AbstractContainer<...> and extract a non-generic Container interface from it:

public interface Container {
    // put non-generic methods here
}
public abstract class AbstractContainer<...> implements Container {
    // put generic methods here
}

Now client code can use Container without carrying about generics, and implementations can extend AbstractContainer<...> and still be able to use/implement generic methods.

Upvotes: 2

Related Questions