You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2020/12/02 18:14:00 UTC
[jira] [Comment Edited] (GROOVY-9844) STC infers too-narrow bounds
for inline map
[ https://issues.apache.org/jira/browse/GROOVY-9844?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17242584#comment-17242584 ]
Eric Milles edited comment on GROOVY-9844 at 12/2/20, 6:13 PM:
---------------------------------------------------------------
If you had {{consume(Map<String, ? extends Object>)}} there should be no error. Otherwise, the compiler infers the type of the map expression only from the map keys/values and not on the target. I believe this is the case as well for {{Map<X,Y> map = [a:1,b:2]}}.
I often need to insert a single typecast in a map expression to get the types right. {{Map<String,Object> map = [a: (Object)1, b:2]}}
was (Author: emilles):
If you had {{consume(Map<String, ? extends Object>)}} there should be no error. Otherwise, the compiler infers the type of the map expression only from the map keys/values and not on the target. I believe this is the case as well for {{Map<X,Y> map = [a:1,b:2]}}.
> 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)