In the groovy documentation, it mentions that using a GString for a key is bad:
def key = 'some key'
def map = [:]
def gstringKey = "${key.toUpperCase()}"
map.put(gstringKey,'value')
assert map.get('SOME KEY') == null
However, simply changing the put() method to use subscript notation:
def key = 'some key'
def map = [:]
def gstringKey = "${key.toUpperCase()}"
map[gstringKey] = 'value' // here
assert map.get('SOME KEY') == null
is enough to cause the assert to fail. How are the semantics any different between using [] and the put() method? Does subscript notation have some sort of implicit cast to String maybe?
GString
as a map key with subscript notation. The conversions to aString
is probably what one would want anyway. The Groovy documentation should instead warn against using theget
andput
methods ofMap
. – Chiasmus