cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 160 forks source link

Support Map initialization via non-Map arguments #546

Closed iffyio closed 4 years ago

iffyio commented 4 years ago

This patch ignores any arguments that isn't of Map type during the invocation of a Map collection. Based on the supported data-structures, these non-Map arguments can either be of type int (capacity, loadFactor) or Comparator. The use case for the latter is not currently supported while the former have no equivalent in javascript.

Fixes #508

lgrignon commented 4 years ago

Thank you very much for your contribution

iffyio commented 4 years ago

@lgrignon This isn't exactly related to this PR :) but I noticed that TreeMap is currently implemented as a regular Map and while looking into that I'm wondering if there is any way to retrieve the actual type of targetExpression while substituting a Map expression - it seems to return the declared type rather than the actual type. For example with tm.put() where tm was declared as Map<String, String> tm = new TreeMap<>(); returns java.util.Map while it returns java.util.TreeMap if tm is declared as TreeMap<String, String> tm = new TreeMap<>();. Is there some mechanism that returns TreeMap in both cases?

lgrignon commented 4 years ago

This is an interesting question. There is currently no way to retrieve the actual type of the target because we do not keep track of the implementation type so we rely on the declared type. We would need - to enable this - to store the map's kind into a field on the underlying JS object at instantiation time. We need to think of a fallback because this field could be missing in Maps coming from outside JSweet (JS lib for instance). Anyway, it is possible but requires a little bit of work. You are not the first person to bring this topic on so please if you really need it, create a dedicated issue, we will think about it with you :)