jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • groovy
  • GROOVY-2660

GStrings and Maps

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

The code

def p = 33
def lista = ["n${ -> p}":'a', 2:'b', 3:'c', 4:'d']
def immutable = lista.asImmutable()

println immutable

println immutable["n33"]

p = 44

println immutable

returns

[n33:"a", 2:"b", 3:"c", 4:"d"]
null
[n44:"a", 2:"b", 3:"c", 4:"d"]
null

The problem is that I cannot retrieve the GString from the map.

The very simple example:

def m = ["n${ -> 33}":'a']

println m["n33"]
println m["n${ -> 33}"]
println m['n${ -> 33}']

println m

None, returns the value of the key if the GString is composed with '$' signs.

Activity

  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
blackdrag blackdrag added a comment - 06/Mar/08 6:48 AM

there where several discussion on the mailing list for the usage of GStrings as map keys. Map keys things that should keep their hashcode the same and their euqals method should also return always the same if feed with the same object. This implies that hash key objects should either not be changed or even better, they should not be mutable. GStrings are mutable. Making a map immutable, does not change that fact. And while the GString "${p}" or "$p" uses the value of p at the time the GString was created and thus could be seen as something that does not change, the GString "${>p}" uses a closure and evaluates the value of p every time anew. Not only that, but the hashcode of "${>p}" differs from "$p", because the hashcode of the closure is used, instead of the hashcode of p to create the GString's hashcode.

So I suggest you to just follow the rule to nether to use a GString as key and there is no issue at all. Even the GString "$p" and "33" do not have the same hashcode for p=33. That is no bug, that is a design decision often discussed on the list. But there are good reasons for it.

Show
blackdrag blackdrag added a comment - 06/Mar/08 6:48 AM there where several discussion on the mailing list for the usage of GStrings as map keys. Map keys things that should keep their hashcode the same and their euqals method should also return always the same if feed with the same object. This implies that hash key objects should either not be changed or even better, they should not be mutable. GStrings are mutable. Making a map immutable, does not change that fact. And while the GString "${p}" or "$p" uses the value of p at the time the GString was created and thus could be seen as something that does not change, the GString "${>p}" uses a closure and evaluates the value of p every time anew. Not only that, but the hashcode of "${>p}" differs from "$p", because the hashcode of the closure is used, instead of the hashcode of p to create the GString's hashcode. So I suggest you to just follow the rule to nether to use a GString as key and there is no issue at all. Even the GString "$p" and "33" do not have the same hashcode for p=33. That is no bug, that is a design decision often discussed on the list. But there are good reasons for it.

People

  • Assignee:
    blackdrag blackdrag
    Reporter:
    ccadete
Vote (0)
Watch (0)

Dates

  • Created:
    05/Mar/08 9:29 AM
    Updated:
    06/Mar/08 6:48 AM
    Resolved:
    06/Mar/08 6:48 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.