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 2019/04/23 17:32:01 UTC

[jira] [Comment Edited] (GROOVY-8212) coerce GString to String when used as Map key

    [ https://issues.apache.org/jira/browse/GROOVY-8212?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16824358#comment-16824358 ] 

Eric Milles edited comment on GROOVY-8212 at 4/23/19 5:31 PM:
--------------------------------------------------------------

One issue caused by Java in this space is that, for legacy reasons, Map's {{get}}, {{containsKey}} and other methods don't use the type parameter {{K}} but instead {{Object}}.  Thus, Java and Groovy have a hard time reasoning about the situation.  Eclipse recently introduced a check for suspicious type passed to {{equals}} because it has the same problem, but the check defaults to an info level message because warning or error was considered too strong.

I think if {{get}} accepted a {{String}}, Groovy would perform the coercion automatically or issue an error in STC mode.


was (Author: emilles):
One issue caused by Java in this space is that, for legacy reasons, Map's {{get}}, {{containsKey}} and other methods don't use the type parameter {{K}} but instead {{Object}}.  Thus, Java and Groovy have a hard time reasoning about the situation.  Eclipse recently introduced a check for suspicious type passed to {{equals}} because it has the same problem, but it defaults to an info level message because warning or error was considered too strong.

I think it {{get}} accepted a {{String}}, Groovy would perform the coercion automatically or issue an error in STC mode.

> coerce GString to String when used as Map key
> ---------------------------------------------
>
>                 Key: GROOVY-8212
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8212
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation, Static Type Checker
>    Affects Versions: 2.4.10
>            Reporter: Christopher Smith
>            Assignee: Daniel Sun
>            Priority: Minor
>
> When a GString is used as the key for a bracket-style map get (in this case, from an overly verbose CSV file):
> {code}
> row["$param URL"]
> {code}
> the {{GStringImpl}} object is passed directly to {{Map#get(Object)}}. Since GStrings are never equal to Strings, this means that the get will always return null.
> If {{row}} is explicitly declared as a {{Map<String,?>}}, however, Groovy ought to identify the intended behavior (using a templated string as a map key) and use the string value instead.
> The current behavior is a problem because even in static compilation mode, where the generic key bound is known, Groovy does not complain about the use of a GString here (because it normally treats GStrings as valid for {{String}} targets?), but the lookup will fail at runtime.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)