java2xtend icon indicating copy to clipboard operation
java2xtend copied to clipboard

Type information can get lost

Open vladdu opened this issue 12 years ago • 3 comments

This snippet

 List<String> x = Lists.newArrayList();

is converted to

var x = Lists.newArrayList

and the type of x is lost.

vladdu avatar May 16 '13 08:05 vladdu

I'm not sure about this one. It's lost alright, but aren't you converting to Xtend because you do not what to explicitly define the types everywhere.

Differenting theses cases from

ArrayList<String> x = Lists.newArrayList();

would only be possible when j2x converter be called by an eclipse plugin (that has access to all the type information). In text only mode the return type of List.newArrayList() is unknown, so it's impossible to detect that it's different that the declared List<String>

rzymek avatar May 16 '13 12:05 rzymek

That's true, but the field/variable has a type declaration in Java and it can be used as it is, I think.

vladdu avatar May 16 '13 12:05 vladdu

I will try to disambiguate a little:

  • Lists.newArrayList is a generic method whose return type depends on the type of the variable it is assitgned to.
  • "var x = Lists.newArrayList" is the same as "List x = Lists.newArrayList();" where we get a generic (and mostly unusable list)
  • in normal xtend usage, one would write "var List<String> x = Lists.newArrayList", which is what I think the converter should output too

vladdu avatar Aug 16 '13 11:08 vladdu