| Summary: | [xbase] [compiler] problem deriving return type when void involved | ||
|---|---|---|---|
| Product: | [Modeling] TMF | Reporter: | Knut Wannheden <knut.wannheden> |
| Component: | Xtext | Assignee: | Project Inbox <tmf.xtext-inbox> |
| Status: | CLOSED FIXED | QA Contact: | |
| Severity: | normal | ||
| Priority: | P3 | CC: | sebastian.zarnekow, sven.efftinge |
| Version: | 2.0.0 | Flags: | sven.efftinge:
indigo+
|
| Target Milestone: | M7 | ||
| Hardware: | All | ||
| OS: | All | ||
| Whiteboard: | |||
I think it should generate:
public String foo(final Object o) {
if ((o instanceof String)) {
return _foo((String)o);
} else if ((o instanceof Object)) {
_foo((Object)o);
return null;
} else {
throw new IllegalArgumentException();
}
}
I'd prefer it to infer return type String for the dispatch methods. Otherwise it would be rather impossible for subtypes to provide another implementation for foo(Object). I agree. So should possibly all the generated _foo() methods also have String as return type? That makes sense. (In reply to comment #3) > I agree. So should possibly all the generated _foo() methods also have String > as return type? Dispatch methods from super types or explicitly declared return types shouldn't be changed. Pushed to master. Closing all bugs that were set to RESOLVED before Neon.0 Closing all bugs that were set to RESOLVED before Neon.0 |
The following Xtend2 example produces invalid Java: class Test { dispatch foo (String o) { o } dispatch foo (Object o) { null } } The following is generated: public String _foo(final String o) { return o; } public Void _foo(final Object o) { return null; } public String foo(final Object o) { if ((o instanceof String)) { return _foo((String)o); } else if ((o instanceof Object)) { return _foo((Object)o); // COMPILE ERROR } else { throw new IllegalArgumentException(); } } Now there is a compile error with the following message: "Type mismatch: cannot convert from Void to String". I think the return type of the dispatch method should be Object or Comparable<? extends Object>. A related use case with basically the same error: class Test { dispatch foo (String o) { o } dispatch foo (Void o) { o } }