If you used exception
outside the conditional operator, then type inference would do the right thing. Here, instead of specifying the type arguments, you can just downcast the result into Value
, either locally for exception()
:
return cond1 ? val1 :
cond2 ? val2 :
cond3 ? val3 :
(Value)exception(new AnyException());
or globally, for the entire conditional expression:
return (Value) (
cond1 ? val1 :
cond2 ? val2 :
cond3 ? val3 :
exception(new AnyException()));
This is still not gold in terms of elegance, but I think it goes a long way. Note that in this case you can have a static import for exception()
.