You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2022/02/26 11:28:00 UTC

[jira] [Resolved] (GROOVY-7802) MapWithDefault should be able to be configured to not store its default value

     [ https://issues.apache.org/jira/browse/GROOVY-7802?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul King resolved GROOVY-7802.
-------------------------------
    Fix Version/s: 5.0.0-alpha-1
                   4.0.1
         Assignee: Paul King
       Resolution: Fixed

Proposed PR merged.

> MapWithDefault should be able to be configured to not store its default value
> -----------------------------------------------------------------------------
>
>                 Key: GROOVY-7802
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7802
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jdk
>    Affects Versions: 2.4.6
>            Reporter: Pap LÅ‘rinc
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 5.0.0-alpha-1, 4.0.1
>
>
> As described in https://github.com/apache/groovy/pull/267, a Groovy map with a default value stores all the accessed elements, even if they weren't added explicitly, just queried.
> In certain algorithms (e.g. https://github.com/careercup/CtCI-6th-Edition-Groovy/blob/d116d65469bdf17d1e215e89f3e76ac3a97660a9/src/main/groovy/Ch04_TreesAndGraphs/_04_12_PathsWithSum.groovy#L21) the complexity would automatically be reduced, if default values would be deleted (i.e. a default of 0 that is incremented and decremented in a recursive algorithm).
> Topics of discussion:
> * Currently a `getAt` will `add` also, therefore `containsKey` isn't the same as `getAt(...) != null`
> * After the change `keySet()/entrySet()` wouldn't include default values
> I proposed alternatives:
> * `removeIfDefault(Object)` // removes current key, if value is equal to default
> * `consolidate()` // removes all default values
> * constructor parameter `STORE_DEFAULT storeDefaultValues = STORE_DEFAULT.FALSE`, or maybe with a `boolean`, though I find primitive parameters confusing
> others:
> * `containsKey` should always return `true`, or throw an exception, since its behavior is inconsistent
> Waiting for your thoughts :)



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