[
https://issues.apache.org/jira/browse/GROOVY-11186?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17771276#comment-17771276
]
Paul King edited comment on GROOVY-11186 at 10/2/23 10:02 PM:
--------------------------------------------------------------
Standalone testcase:
{code:java}
def odds = [1, 3]
def evens = [2, 4]
assert [*odds, *evens].sort() == 1..4 // Okay
assert [*odds.toSet(), *evens.toSet()].sort() == 1..4 //
IllegalArgumentException
{code}
Currently, the spread operator works on lists and arrays (and {{null}}) which
have a well-defined order:
https://github.com/apache/groovy/blob/master/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java#L930-L936
I guess we could also support Set or indeed Iterable. Then it would be up to
users to use a class that has a well-defined order, if that is what they need
from the spread result.
was (Author: paulk):
Standalone testcase:
{code:java}
def odds = [1, 3]
def evens = [2, 4]
assert [*odds, *evens].sort() == 1..4
assert [*odds.toSet(), *evens.toSet()].sort() == 1..4
{code}
Currently, the spread operator works on lists and arrays (and {{null}}) which
have a well-defined order:
https://github.com/apache/groovy/blob/master/src/main/java/org/codehaus/groovy/runtime/ScriptBytecodeAdapter.java#L930-L936
I guess we could also support Set or indeed Iterable. Then it would be up to
users to use a class that has a well-defined order, if that is what they need
from the spread result.
> permit spread operator on Set
> -----------------------------
>
> Key: GROOVY-11186
> URL: https://issues.apache.org/jira/browse/GROOVY-11186
> Project: Groovy
> Issue Type: Improvement
> Affects Versions: 4.0.13
> Reporter: Christopher Smith
> Priority: Major
>
> In a Spock test case, I am trying to merge the e-mail addresses for two test
> users, where {{emails}} is of declared type {{java.util.Set}}:
> {code}
> methodCall([*BOB.emails, *BRAD.emails])
> {code}
> I expected the spread to inline the values, but instead I get
> {code}
> java.lang.IllegalArgumentException: cannot spread the type
> java.util.LinkedHashSet with value [[email protected]]
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)