The code:
interface Property<T>
{
T get();
}
class BoolProperty implements Property<Boolean>
{
@Override
public Boolean get()
{
return false;
}
}
class StringProperty implements Property<String>
{
@Override
public String get()
{
return "hello";
}
}
class OtherStringProperty implements Property<String>
{
@Override
public String get()
{
return "bye";
}
public String getSpecialValue()
{
return "you are special";
}
}
is used by my class:
class Result<P extends Property<X>, X>
{
P p;
List<X> list;
}
As you see it has two type parameters P and X. Despite of that the X can always be deduced from P but the language requires me to supply both:
Result<BooleanProperty, Boolean> res = new Result<BooleanProperty, Boolean>();
Is there any trick to get rid of the X type parameter? I want just use
Result<BooleanProperty> res = new Result<BooleanProperty>();
Also, I don’t want lose type information and use it as:
Result<OtherStringProperty> res = new Result<OtherStringProperty>();
String spec = res.p.getSpecialValue();
String prop = res.list.get(0);
There is a similar question whose answer you might find interesting: https://stackoverflow.com/a/4452268/247763
Essentially, there’s no real way to get around including the extra generic type, because the compiler can’t know what type you’re using without it. I’m guessing this defeats the purpose of your approach, but you could try extending
Resultwhile specifying the types – something like this: