You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/02/26 12:29:41 UTC
[groovy] branch master updated: GROOVY-10508: DefaultGroovyMethods.get() unexpectedly puts the default value into the origin map
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new e6fcb0a GROOVY-10508: DefaultGroovyMethods.get() unexpectedly puts the default value into the origin map
e6fcb0a is described below
commit e6fcb0ab7451ad3ce60099f1d3ec159061fba8b3
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Feb 26 22:25:42 2022 +1000
GROOVY-10508: DefaultGroovyMethods.get() unexpectedly puts the default value into the origin map
---
.../org/codehaus/groovy/runtime/DefaultGroovyMethods.java | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index e56062d..3f8c806 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -7865,12 +7865,17 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
}
/**
- * Looks up an item in a Map for the given key and returns the value - unless
- * there is no entry for the given key in which case add the default value
- * to the map and return that.
- * <pre class="groovyTestCase">def map=[:]
+ * Looks up an item in a Map for the given key and returns the corresponding value.
+ * If there is no entry for the given key return instead the default value and
+ * also add the key and default value to the map.
+ * <pre class="groovyTestCase">
+ * def map=[:]
* map.get("a", []) << 5
- * assert map == [a:[5]]</pre>
+ * assert map == [a:[5]]
+ * </pre>
+ * For a method which doesn't mutate the map, consider instead using {@link Map#getOrDefault(Object, Object)}
+ * or consider using Groovy's {@link MapWithDefault} often instantiated using {@link #withDefault(Map, Closure)}
+ * or with more options {@link #withDefault(Map, boolean, boolean, Closure)}.
*
* @param map a Map
* @param key the key to lookup the value of