ipp: Small intention bug

Hi.

ipp has an intention to turn an if to a ternary operator.
Sometimes the result does not compile because of a limitation of the ternary
operator.
When it is used, the result of one expression must be castable to the other
one (the order doesn't matter).

Example:
class P {}
class A extends P {}
class B extends P {}
void foo() {
A a = new A();
B b = new B();
P p;
if (true) {
p = a;
} else {
p = b;
}
}

When the intention is used, the result is:
class P {}
class A extends P {}
class B extends P {}
void foo() {
A a = new A();
B b = new B();
P p;
p = true ? a : b;
}

It can't compile and IntelliJ is correct when it complains.

I think that this case should be detected.

Amnon


4 comments
Comment actions Permalink


Wow, you're right. Ternary's only work if the type of one of the branches is assignment compatible with the type of the other. I had assumed that it would work if they had a common supertype, and that the type of the ternary would be the least common supertype.

What an extremely odd semantic.

The fix will be in version 3, which will go out as soon as I finish driving wooden stakes through the corroded, twitching hearts of the switch<->if conversions. Wednesday, maybe.

--Dave "I swear if I keep on like this I'm gonna end up memorizing the JLS" Griffith

0
Comment actions Permalink

Awsome. I wonder what's the reason behind this limitation. I always thought
(until yesterday) that the ternary is as capable as an if...

Thanks,
Amnon

"Dave Griffith" <dave.griffith@cnn.com> wrote in message
news:20416442.1054932755048.JavaMail.itn@is.intellij.net...
>

Wow, you're right. Ternary's only work if the type of one of the branches

is assignment compatible with the type of the other. I had assumed that it
would work if they had a common supertype, and that the type of the ternary
would be the least common supertype.
>

What an extremely odd semantic.

>

The fix will be in version 3, which will go out as soon as I finish

driving wooden stakes through the corroded, twitching hearts of the
switch<->if conversions. Wednesday, maybe.
>

--Dave "I swear if I keep on like this I'm gonna end up memorizing the

JLS" Griffith


0
Comment actions Permalink

Dave Griffith wrote:

Wow, you're right. Ternary's only work if the type of one of the
branches is assignment compatible with the type of the other.


This reminds me it would be nice to have an intention to insert the (P)
casts in both branches of a ternary operator if there's a type error
there (I've come across this a number of times in practice already). I
guess I should enter a tracker request unless one already exists, but
I'm too tired to search for duplicates right now. Have to go to bed soon...

0
Comment actions Permalink

Actually, casting even one of them to P is enough...

"Jonas Kvarnstr?m" <jonkv@ida.liu.se> wrote in message
news:bbr4i1$csh$1@is.intellij.net...

Dave Griffith wrote:

>

Wow, you're right. Ternary's only work if the type of one of the
branches is assignment compatible with the type of the other.

>

This reminds me it would be nice to have an intention to insert the (P)
casts in both branches of a ternary operator if there's a type error
there (I've come across this a number of times in practice already). I
guess I should enter a tracker request unless one already exists, but
I'm too tired to search for duplicates right now. Have to go to bed

soon...
>


0

Please sign in to leave a comment.