[
https://issues.apache.org/jira/browse/GROOVY-9844?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17243553#comment-17243553
]
Eric Milles commented on GROOVY-9844:
-------------------------------------
I was describing the current behavior/design. In general, not a lot of
attention is paid to the LHS type for assignment or method call when examining
the RHS. There is a bit of chicken-egg problem when it comes to method
overload selection. In order to make the best method selection the argument
types must be know. However in order to best infer the argument types, the
method must be known. The compiler opts to infer the argument types first
(except for closures), and so you get String for both key and value types.
By adjusting the method parameter type to accept "? extends Object" or "? super
Object" -- depending on your situation -- for the value type, you don't need
the compiler or your caller to provide exactly {{Map<String,Object>}}.
> STC infers too-narrow bounds for inline map
> -------------------------------------------
>
> Key: GROOVY-9844
> URL: https://issues.apache.org/jira/browse/GROOVY-9844
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 3.0.6
> Reporter: Christopher Smith
> Priority: Minor
>
> I thought this had been resolved, but I just ran into it again.
> The static type checker always infers the strictest possible generic bounds
> for an inline map.
> {code:groovy}
> void consume(Map<String, Object> map) {}
> void run() {
> consume([key: 'value']) // compilation fails
> }
> {code}
> {code}
> Cannot call example#consume(java.util.Map <java.lang.String,
> java.lang.Object>) with arguments [java.util.LinkedHashMap <java.lang.String,
> java.lang.String>]
> {code}
> Explicitly causing the STC to "snap out of it" by adding {{as Map}} (no
> generics required) works.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)