Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Why is a return type erased
Why is a return type erased [message #997607] Mon, 07 January 2013 10:26 Go to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<font size="3">I'd open a bugzilla, but Javac and JDT agree that the
following code contains
an error</font>
so it must behave this way for a reason (that's inexplicable to me):<br>
<br>
<font size="3">public class TestRawInheritance<br>
{<br>
&nbsp; class Base&lt;T&gt;<br>
&nbsp; {<br>
&nbsp;&nbsp;&nbsp; //<br>
&nbsp; }<br>
<br>
&nbsp; class Derived extends Base<br>
&nbsp; {<br>
&nbsp;&nbsp;&nbsp; //<br>
&nbsp; }<br>
<br>
&nbsp; class Consumer&lt;T1&gt;<br>
&nbsp; {<br>
&nbsp;&nbsp;&nbsp; &lt;T&gt; T1 foo(Base&lt;T&gt; base)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; T1 bar(Base base)<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; T1 test()<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foo(new Derived());<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T1 result = foo(new Derived());<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result = bar(new Derived());<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp; }<br>
&nbsp; <br>
&nbsp; public void test()<br>
&nbsp; {<br>
&nbsp;&nbsp;&nbsp; Boolean result = new Consumer&lt;Boolean&gt;().foo(new
Derived());<br>
&nbsp; }<br>
}</font><br>
<font size="3"><br>
Specifically the line "T1 result = foo(new Derived());" is marked
with the error "Type mismatch: cannot convert from Object to T1",
but it seems counter intuitive that a method that returns T1 ought
to ever
to be interpreted such that it returns Object other than in a
context where
Consumer itself is a raw type. &nbsp; I assume it's because Derived
itself
is a raw type with respect to Base, but<font size="3"> </font>why
does that affect the return type of this method? <font size="3">And
why isn't the ca<font size="3">ll to bar an error?</font></font><br>
<br>
Also, why is this not an error?<br>
<br>
&nbsp;public void test()<br>
&nbsp;{<br>
&nbsp; &nbsp;Boolean result = new Consumer&lt;Boolean&gt;().foo(new
Derived());<br>
&nbsp;}<br>
<br>
EMF generates code similar to this, i.e., the XyzSwitch class, and
I only just noticed these errors in an
ugly test model...<br>
</font>
</body>
</html>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Why is a return type erased [message #997610 is a reply to message #997607] Tue, 08 January 2013 11:15 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
On 07.01.2013 11:26, Ed Merks wrote:
> I'd open a bugzilla, but Javac and JDT agree that the following code
> contains an error so it must behave this way for a reason (that's
> inexplicable to me):
>
> public class TestRawInheritance
> {
> class Base<T>
> {
> //
> }
>
> class Derived extends Base
> {
> //
> }
>
> class Consumer<T1>
> {
> <T> T1 foo(Base<T> base)
> {
> return null;
> }
>
> T1 bar(Base base)
> {
> return null;
> }
>
> T1 test()
> {
> foo(new Derived());
> T1 result = foo(new Derived());
> result = bar(new Derived());
> return result;
> }
> }
>
> public void test()
> {
> Boolean result = new Consumer<Boolean>().foo(new Derived());
> }
> }
>
> Specifically the line "T1 result = foo(new Derived());" is marked with
> the error "Type mismatch: cannot convert from Object to T1", but it
> seems counter intuitive that a method that returns T1 ought to ever to
> be interpreted such that it returns Object other than in a context
> where Consumer itself is a raw type. I assume it's because Derived
> itself is a raw type with respect to Base, but why does that affect
> the return type of this method? And why isn't the call to bar an error?
This looks like a bug in the inference code (both Oracle and Eclipse
compiler). It gets confused by the raw type. As a workaround you can
- add a cast
- parametrize it with <Object>
- use the diamond (1.7 and beyond)

Please file a bug against JDT Core and the Oracle JDK.
>
> Also, why is this not an error?
Here, the inference already knows T1=Boolean and hence doesn't fail i.e.
doesn't try do infer it.
>
> public void test()
> {
> Boolean result = new Consumer<Boolean>().foo(new Derived());
> }
>
> EMF generates code similar to this, i.e., the XyzSwitch class, and I
> only just noticed these errors in an ugly test model...
And ugly generated code ;-).

Dani
Re: Why is a return type erased [message #997755 is a reply to message #997610] Tue, 08 January 2013 17:14 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
I've opened https://bugs.eclipse.org/bugs/show_bug.cgi?id=397675 if
anyone is interested. I reported an Oracle JDK bug as well with the
response "Your report has been assigned an internal review ID of
2421492". I'll add the Oracle link to the bugzilla when I get one.


On 08/01/2013 12:15 PM, Daniel Megert wrote:
> On 07.01.2013 11:26, Ed Merks wrote:
>> I'd open a bugzilla, but Javac and JDT agree that the following code
>> contains an error so it must behave this way for a reason (that's
>> inexplicable to me):
>>
>> public class TestRawInheritance
>> {
>> class Base<T>
>> {
>> //
>> }
>>
>> class Derived extends Base
>> {
>> //
>> }
>>
>> class Consumer<T1>
>> {
>> <T> T1 foo(Base<T> base)
>> {
>> return null;
>> }
>>
>> T1 bar(Base base)
>> {
>> return null;
>> }
>>
>> T1 test()
>> {
>> foo(new Derived());
>> T1 result = foo(new Derived());
>> result = bar(new Derived());
>> return result;
>> }
>> }
>>
>> public void test()
>> {
>> Boolean result = new Consumer<Boolean>().foo(new Derived());
>> }
>> }
>>
>> Specifically the line "T1 result = foo(new Derived());" is marked
>> with the error "Type mismatch: cannot convert from Object to T1", but
>> it seems counter intuitive that a method that returns T1 ought to
>> ever to be interpreted such that it returns Object other than in a
>> context where Consumer itself is a raw type. I assume it's because
>> Derived itself is a raw type with respect to Base, but why does that
>> affect the return type of this method? And why isn't the call to bar
>> an error?
> This looks like a bug in the inference code (both Oracle and Eclipse
> compiler). It gets confused by the raw type. As a workaround you can
> - add a cast
> - parametrize it with <Object>
> - use the diamond (1.7 and beyond)
>
> Please file a bug against JDT Core and the Oracle JDK.
>>
>> Also, why is this not an error?
> Here, the inference already knows T1=Boolean and hence doesn't fail
> i.e. doesn't try do infer it.
>>
>> public void test()
>> {
>> Boolean result = new Consumer<Boolean>().foo(new Derived());
>> }
>>
>> EMF generates code similar to this, i.e., the XyzSwitch class, and I
>> only just noticed these errors in an ugly test model...
> And ugly generated code ;-).
>
> Dani


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:code markers - how are they implemented?
Next Topic:PlatformUI.createAndRunWorkbench(..) does not return in JUnit plug-in tests
Goto Forum:
  


Current Time: Thu Apr 25 11:27:26 GMT 2024

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

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

Back to the top