Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » compiler bug with ternary operator and typed static method
compiler bug with ternary operator and typed static method [message #515486] Thu, 18 February 2010 17:50 Go to next message
Eclipse UserFriend
i'm posting this in the forum first as i found nothing in bugzilla with some of the keywords from this topic's title.

i'm running eclipse 3.5

this code causes a compiler error on the last line
List<RemotePageSummary> getChildren(long parentId) {
    final List<RemotePageSummary> childrenList = parentToChildPageMap.get(new Long(parentId));
    return childrenList == null ? Collections.emptyList() : childrenList;
  }


while this doesnt

List<RemotePageSummary> getChildren(long parentId) {
    final List<RemotePageSummary> childrenList = parentToChildPageMap.get(new Long(parentId));
    if (childrenList == null) {
      return Collections.emptyList();
    } else {
      return childrenList;
    }
  }


is this a bug or is there a good reason for the compiler to complain ?

Re: compiler bug with ternary operator and typed static method [message #515531 is a reply to message #515486] Fri, 19 February 2010 04:57 Go to previous messageGo to next message
Eclipse UserFriend
This is a multi-part message in MIME format.
--------------050909090102040407090308
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Thomas

What error message are you getting?

On 18/02/2010 22:50, thomas menzel wrote:
> return childrenList == null ? Collections.emptyList() : childrenList;
What happens if you use

return ((childrenList == null) ? Collections.emptyList() : childrenList);


instead?

Steffen

--------------050909090102040407090308
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Thomas<br>
<br>
What error message are you getting?<br>
<br>
On 18/02/2010 22:50, thomas menzel wrote:
<blockquote cite="mid:hlkg8e$8np$1@build.eclipse.org" type="cite">return
childrenList == null ? Collections.emptyList() : childrenList;
</blockquote>
What happens if you use<br>
<blockquote>
<pre>return ((childrenList == null) ? Collections.emptyList() : childrenList);
</pre>
</blockquote>
instead?<br>
<br>
Steffen<br>
</body>
</html>

--------------050909090102040407090308--
Re: compiler bug with ternary operator and typed static method [message #515535 is a reply to message #515531] Fri, 19 February 2010 05:05 Go to previous messageGo to next message
Eclipse UserFriend
the error i get is

Type mismatch: cannot convert from List<capture#1-of ? extends Object> to List<RemotePageSummary>

which is also the same for your snippet
Re: compiler bug with ternary operator and typed static method [message #515628 is a reply to message #515486] Fri, 19 February 2010 09:55 Go to previous messageGo to next message
Eclipse UserFriend
Le 2010-02-18 17:50, thomas menzel a écrit :
> this code causes a compiler error on the last line
> List<RemotePageSummary> getChildren(long parentId) {
> final List<RemotePageSummary> childrenList =
> parentToChildPageMap.get(new Long(parentId));
> return childrenList == null ? Collections.emptyList() : childrenList;
> }
> is this a bug or is there a good reason for the compiler to complain ?
I don't think this is a bug.
When you use the conditional expression (?:), the resulting type is:
List<a capture of ? extends java.lang.Object>.

Then this is not assignment compatible with List<RemotePageSummary>.


When you use an if statement, the return Collections.emptyList() type is
List<X> and no longer List<a capture of ? extends java.lang.Object>.

Both types are then compatible and there is no compile error.

Note that javac agrees with us and also doesn't compile the snippet
using the conditional expression.
--
Olivier
Re: compiler bug with ternary operator and typed static method [message #515633 is a reply to message #515628] Fri, 19 February 2010 10:14 Go to previous messageGo to next message
Eclipse UserFriend
Olivier Thomann wrote on Fri, 19 February 2010 15:55

When you use the conditional expression (?Smile, the resulting type is:
List<a capture of ? extends java.lang.Object>.



why is this so?
is that defined in the java spec?

i would have assumed that there is no semantic diff. in using a ternary operator vs. an if/else construct.

in the end, each part needs to be assignment compatible with the LHS and hence the generic type is known.
Re: compiler bug with ternary operator and typed static method [message #515649 is a reply to message #515633] Fri, 19 February 2010 11:06 Go to previous messageGo to next message
Eclipse UserFriend
When you are using a conditional expression, the resulting type of the conditional expression is the lower upper bound of the two operands.
This is not the case when you use an if statement where you only need to check assignment compatibility of each return statement type with the type of the method.
You should check:
http://java.sun.com/docs/books/jls/third_edition/html/expres sions.html#341287
and:
http://java.sun.com/docs/books/jls/third_edition/html/expres sions.html#15.25

Hope this clarify it.

Olivier
Re: compiler bug with ternary operator and typed static method [message #515669 is a reply to message #515486] Fri, 19 February 2010 12:19 Go to previous messageGo to next message
Eclipse UserFriend
> this code causes a compiler error on the last line
> List<RemotePageSummary> getChildren(long parentId) {
> final List<RemotePageSummary> childrenList =
> parentToChildPageMap.get(new Long(parentId));
> return childrenList == null ? Collections.emptyList() : childrenList;
> }

The compiler cannot infer the right type for Collections.emptyList(),
but you can help it by adding explicit type arguments like this:
Collections.<RemotePageSummary>emptyList()

Markus
Re: compiler bug with ternary operator and typed static method [message #515819 is a reply to message #515669] Sun, 21 February 2010 07:50 Go to previous message
Eclipse UserFriend
thx for clearing this up.

learned smth new again!
Previous Topic:Accessing help causes network error (tcp_error)
Next Topic:Java debugging - cannot be resolved
Goto Forum:
  


Current Time: Mon Apr 14 19:07:25 EDT 2025

Powered by FUDForum. Page generated in 0.03672 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top