Community
Participate
Working Groups
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 } }
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