Hi everyone.
I have the following code, which works without problem:
if (list.get(0) instanceof A) {
something = ((A) list.get(0)).getSomething();
} else {
something = ((B) list.get(0)).getSomething();
}
...but my eyes hurt when I see it: two identical lines save for the type casting. So I tried this:
something = ((list.get(0) instanceof A? (A) : (B)) list.get(0)).getSomething();
...and it doesn't work! It gives me the following errors:
something = ((list.get(0) instanceof A? *(A)* : (B)) list.get(0)).getSomething();
A cannot be resolved
something = ((list.get(0) instanceof A? (A) : *(B)* ) list.get(0)).getSomething();
B cannot be resolved
something = ((list.get(0) instanceof A? (A) : (B))
list.get(0)).getSomething();
Syntax error on token "list", delete this token
Is it impossible to use the ternary operator here? I tried several combinations of parenthesis, just in case, unsuccessfully.
Thanks!