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/07/17 16:40:00 UTC

[jira] [Commented] (GROOVY-9617) map.put(key,value) and map[key] = value differ is key is a GString

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

Eric Milles commented on GROOVY-9617:
-------------------------------------

The subscript operation {{map[key]}} has 2 potential resolutions at runtime:
{code}
    void putAt(Object self, String property, Object newValue)
    <K,V> V putAt(Map<K,V> self, K key, V value)
{code}

When the parameter distance algorithm is applied, it is actually the Object,String,Object method that is selected when your key is a GStringImpl.  This is implemented by {{groovy.lang.MetaClassImpl#doChooseMostSpecificParams}}.

> map.put(key,value) and map[key] = value differ is key is a GString
> ------------------------------------------------------------------
>
>                 Key: GROOVY-9617
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9617
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.5.10
>            Reporter: Laurent Martelli
>            Priority: Minor
>
> The subscript operator of Map transform {{GString}} keys to {{String. }}But {{put()}} does not. This is incoherent.
>  
> {code:java}
> class App {
>     static void main(String[] args) {
>         println("subscript works: "+subscriptWorks())
>         println("put works: "+putWorks())
>     }
>     static def subscriptWorks() {
>         def map = [:]
>         def x = "toto"
>         def key = "$x"
>         map[key] = "value"
>         return map.containsKey(key)
>     }
>     static def putWorks() {
>         def map = [:]
>         def x = "toto"
>         def key = "$x"
>         map.put(key,"value")
>         return map.containsKey(key)
>     }
> }
> {code}
> Outputs this :
> {code:java}
> subscript works: false
> subscript works: true
> {code}
> It would be better to either cast all key parameters of all methods from GString to String or none. The current behaviour is very confusing.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)