The subscript syntax for maps complains about a read-only property named "properties"
"Costanzo, David I" <[email protected]> Mon, 13 Apr 2026 18:32:11 +0000
| Newsgroups | gmane.comp.lang.groovy.user |
|---|---|
| Message-ID | <SA1PR11MB8860F38C1A4ADF338AFDCE5CC3242@SA1PR11MB8860.namprd11.prod.outlook.com> |
--_000_SA1PR11MB8860F38C1A4ADF338AFDCE5CC3242SA1PR11MB8860namp_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
I have a problem with maps and I like some input from Groovy experts to und=
erstand if it's a bug in my Groovy script or a bug in Groovy.
I use the subscript syntax for accessing elements in a map, as in:
def things =3D [:]
things['mything'] =3D 1
println things['mything']
I recently upgraded a script from Groovy 3.0.10 to Groovy 5.0.5 and my scri=
pt broke because it happened to use a key named "properties". In this case=
, the keys are determined programmatically and represent the names of inter=
nal libraries. I was able to fix my script by using the methods of Map: pu=
t(), get(), but that feels ungroovy and I'd like to continue using the subs=
cript syntax.
This can be seen in groovysh from Groovy 5.0.5
groovy> map =3D [:]
groovy> map['properties'] =3D 1
ReadOnlyPropertyException: Cannot set read-only property: properties for =
class: java.util.LinkedHashMap
groovy> map['properties']
[:]
Whereas I expect behavior similar to what I see in Groovy 3.0.10, where map=
['properties'] can be used to add and read an element.
groovy:000> map =3D [:]
=3D=3D=3D> [:]
groovy:000> map['properties'] =3D 1
=3D=3D=3D> 1
groovy:000> map['properties']
=3D=3D=3D> 1
The documentation for Map at https://groovy-lang.org/groovy-dev-kit.html#_a=
dding_or_removing_elements_2 says that elements can be added with the subsc=
ript operator, so I didn't think I was doing anything wrong.
Am I wrong to use the subscript operator if I don't know for sure that my k=
eys won't conflict with a property of Map? Or is this a bug in Groovy 5.0.=
5?
-David Costanzo
--_000_SA1PR11MB8860F38C1A4ADF338AFDCE5CC3242SA1PR11MB8860namp_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Aptos;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:12.0pt;
font-family:"Aptos",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#467886;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Aptos",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang=3D"EN-US" link=3D"#467886" vlink=3D"#96607D" style=3D"word-wrap:=
break-word">
<div class=3D"WordSection1">
<p class=3D"MsoNormal"><span style=3D"color:black">I have a problem with ma=
ps and I like some input from Groovy experts to understand if it’s a =
bug in my Groovy script or a bug in Groovy.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black">I use the subscript synt=
ax for accessing elements in a map, as in:<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black"> def t=
hings =3D [:]<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> things['my=
thing'] =3D 1<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> println th=
ings['mything']<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black">I recently upgraded a sc=
ript from Groovy 3.0.10 to Groovy 5.0.5 and my script broke because it happ=
ened to use a key named "properties". In this case, the key=
s are determined programmatically and represent the
names of internal libraries. I was able to fix my script by using th=
e methods of Map: put(), get(), but that feels ungroovy and I’d like =
to continue using the subscript syntax.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black">This can be seen in groo=
vysh from Groovy 5.0.5<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black"> groovy> map =
=3D [:]<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> groovy> map['p=
roperties'] =3D 1<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> ReadOnlyPropertyE=
xception: Cannot set read-only property: properties for class: java.util.Li=
nkedHashMap<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> groovy> map['p=
roperties'] <o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> [:]<o:p></o:p></s=
pan></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black">Whereas I expect behavio=
r similar to what I see in Groovy 3.0.10, where map['properties'] can be us=
ed to add and read an element.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black"> groovy:000&=
gt; map =3D [:]<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> =3D=3D=3D&g=
t; [:]<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> groovy:000&=
gt; map['properties'] =3D 1<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> =3D=3D=3D&g=
t; 1<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> groovy:000&=
gt; map['properties']<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"> =3D=3D=3D&g=
t; 1<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black">The documentation for Ma=
p at <a href=3D"https://groovy-lang.org/groovy-dev-kit.html#_adding_or_remo=
ving_elements_2">
https://groovy-lang.org/groovy-dev-kit.html#_adding_or_removing_elements_2<=
/a> says that elements can be added with the subscript operator, so I =
didn't think I was doing anything wrong.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black">Am I wrong to use the su=
bscript operator if I don't know for sure that my keys won't conflict with =
a property of Map? Or is this a bug in Groovy 5.0.5?<o:p></o:p></span=
></p>
<p class=3D"MsoNormal"><span style=3D"color:black"><o:p> </o:p></span>=
</p>
<p class=3D"MsoNormal"><span style=3D"color:black">-David Costanzo</span><s=
pan style=3D"font-size:11.0pt"><o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:11.0pt"><o:p> </o:p></=
span></p>
</div>
</body>
</html>
--_000_SA1PR11MB8860F38C1A4ADF338AFDCE5CC3242SA1PR11MB8860namp_--