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

Shi Tailong commented on GROOVY-10508:
--------------------------------------

Alas, yes you are right. It makes sense in this case.

> DefaultGroovyMethods.get() should not put the default value into the origin 
> map
> -------------------------------------------------------------------------------
>
>                 Key: GROOVY-10508
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10508
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Shi Tailong
>            Priority: Minor
>
>  
> {code:java}
> public static <K, V> V get(Map<K, V> map, K key, V defaultValue) {
>     if (!map.containsKey(key)) {
>         map.put(key, defaultValue);
>     }
>     return map.get(key);
> } {code}
> Above there's the get() method for ALL maps if there's more than one 
> arguments for this. You can find that it will put the defaultValue into the 
> map if the key doesn't hit.
>  
> It seems not to be an obvious choice for programmers, as the origin map would 
> be CHANGED when the programmer may just want a default value.
> Meanwhile, some of the implements of Map have some restrictions for the 
> values. For example, Redisson doesn't allow null values for its RMap class. 
> So when you're using Redisson in Groovy like this,
>  
> {code:java}
> RMapCache<String, String> map = redissonClient.getMapCache(SOME_KEY)
> String cachedValue = map.getOrDefault(key, null) {code}
> it will return a NullPointerError like "map value can't be null".
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to