Bug 488011 - Dispatch methods are not intuitive when a primitive type is involved
Summary: Dispatch methods are not intuitive when a primitive type is involved
Status: NEW
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.9.1   Edit
Hardware: PC Windows 7
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-18 07:19 EST by Norbert Sándor CLA
Modified: 2016-02-18 08:14 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Norbert Sándor CLA 2016-02-18 07:19:28 EST
The following test fails that is not intuitive:

class TestDispatch {
  def dispatch method(int value) { "int" }
	
  def dispatch method(Object value) { "Object" }
	
  @Test
  def void test() {
    assertEquals("int", method(1)) // Fails: expected:<[in]t> but was:<[Objec]t>
  }	
}

The generated code is:

  public String method(final Object value) {
    if (value instanceof Integer) {
      return _method((Integer)value); // 1
    } else if (value != null) {
      return _method(value); // 2
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(value).toString());
    }
  }

The problem is that at runtime both 1 and 2 dispatches to method(Object), method(int) is never called.

This may be a fundamental problem with primitive parameters in dispatch methods: maybe primitives should not be allowed at all, or should be dispatched always statically...
Comment 1 Jan Koehnlein CLA 2016-02-18 08:14:27 EST
At least a warning would be nice.