Why is a return type erased [message #997607] |
Mon, 07 January 2013 10:26 |
Ed Merks 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>
class Base<T><br>
{<br>
//<br>
}<br>
<br>
class Derived extends Base<br>
{<br>
//<br>
}<br>
<br>
class Consumer<T1><br>
{<br>
<T> T1 foo(Base<T> base)<br>
{<br>
return null;<br>
}<br>
<br>
T1 bar(Base base)<br>
{<br>
return null;<br>
}<br>
<br>
T1 test()<br>
{<br>
foo(new Derived());<br>
T1 result = foo(new Derived());<br>
result = bar(new Derived());<br>
return result;<br>
}<br>
}<br>
<br>
public void test()<br>
{<br>
Boolean result = new Consumer<Boolean>().foo(new
Derived());<br>
}<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. 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>
public void test()<br>
{<br>
Boolean result = new Consumer<Boolean>().foo(new
Derived());<br>
}<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 |
Dani Megert 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 |
Ed Merks 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/
|
|
|
Powered by
FUDForum. Page generated in 0.04431 seconds