• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using interface with ?: operator  RSS feed

 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't this work:






Seems the compiler should be able to figure out that both A and B are of type Foo, and assign the result.. Is there a good reason why you can't do this?

Thanks
Geoffrey
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Geoffrey Falk:

Seems the compiler should be able to figure out that both A and B are of type Foo, and assign the result..


Yes, it could -- and probably should -- but it doesn't. It's just not defined that way in the Java language spec.

You can do this:

Foo result = (x) ? (Foo) new A() : (Foo) new B();
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try casting...

 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
couldn't you alternatively do:

?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Beck:
couldn't you alternatively do:

?


No. It's not the assigment to "result" that's the problem; it won't compile the conditional expression itself because it doesn't consider common interfaces in resolving the type of the whole expression.
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found an explanation of this here. The problem is, the compiler needs to assign a definite type to the expression:



We know from the left hand side that we need a Foo, but the compiler can't tell that from the expression itself! If A and B implemented some other interface as well (say Serializable) then is the ternary expression of type Foo? Or type Serializable? It can't be both.

PS: I found that this code actually does compile under JDK 1.5!

Geoffrey

[ April 26, 2005: Message edited by: Geoffrey Falk ]

[ April 26, 2005: Message edited by: Geoffrey Falk ]
[ April 26, 2005: Message edited by: Geoffrey Falk ]
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the clarification. but now i'm all interested in how and why Java 5 manages to compile it - is that a feature of autoboxing, perhaps?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some specific semantic changes for Tiger. Basically, it now works the way you'd like it to. This isn't related to autoboxing, but instead it's actually part of the generics support.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Carman:
Try casting...



Actually, you only really need to cast one of them...



The compiler will know that the type of the expression needs to be Foo and since B implements Foo, you're okay.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!