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 2023/08/01 13:33:00 UTC

[jira] [Commented] (GROOVY-10893) collectEntries accepts one or more entry values for each iteration but not zero (via null)

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

Paul King commented on GROOVY-10893:
------------------------------------

The idiomatic way currently for something like {{collectMany}} would be:
{code:java}
list.collectMany{ x ? y : [] }
{code}
But I see no issue with your suggestion. 

> collectEntries accepts one or more entry values for each iteration but not zero (via null)
> ------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-10893
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10893
>             Project: Groovy
>          Issue Type: Improvement
>          Components: groovy-jdk
>            Reporter: Eric Milles
>            Assignee: Eric Milles
>            Priority: Minor
>
> The {{collectEntries}} extension methods accept one entry (as an entry, array, list or map).  And it accepts multiple entries (via a map).  However, it does not let you return no entry (as null).  You can probably return an empty map...
> Similar to {{findResults}}, I'd like to be able to conditionally return an entry or entries:
> {code:groovy}
> def map = list.collectEntries { item ->
>   if (item ...) {
>     def key = ..., val = ...;
>     Collections.singletonMap(key, val)
>   }
> }
> {code}
> {{org.codehaus.groovy.runtime.DefaultGroovyMethods#addEntry(Map,Object)}} could check for null before calling {{asType(newEntry, Map.Entry.class)}}.
> {{collectMany}} has a similar issue.  I need to write "if \(x\) y else Collections.emptyList()" instead of just "if \(x\) y" in the closure block.  A simple null check in {{org.codehaus.groovy.runtime.DefaultGroovyMethods#collectMany(Iterable,Collection,Closure)}} could take care of this.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)