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

Paul King commented on GROOVY-10666:
------------------------------------

I am not sure we have this quite right yet. I haven't thought through whether 
the two paths cause an issue for code coverage but I think iterator() having 
higher priority is problematic. I think many folks would find the behavior of 
the script below a little surprising:
{code}
class Foo {
    Iterator iterator() { [1, 2].iterator() }
    def getAt(int i) { i * 42 }
}

def f = new Foo()
def (x, y) = f
assert x + y == 3
assert f[0] + f[1] == 42
{code}
Also, when the optional tuple-based components() accessor was added for our 
records, I was thinking something like a getAtAll() returning a tuple might be 
something for us to consider in the future for better type safety with a 
multi-assign. I would imagine it would be higher priority than iterator() too. 
We haven't designed that yet, so we can't really use that against the idea of 
iterator() support, but it would be a bonus if we could think of a way to make 
it future compatible.
 

> Implement multiple-assignment (aka destructuring) via getAt(IntRange) or 
> iterator()
> -----------------------------------------------------------------------------------
>
>                 Key: GROOVY-10666
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10666
>             Project: Groovy
>          Issue Type: Improvement
>          Components: bytecode, groovy-jdk
>            Reporter: Eric Milles
>            Assignee: Eric Milles
>            Priority: Minor
>
> Currently multiple-assignment is implemented using {{getAt(int)}}.  For types 
> that support {{getAt(IntRange)}} it may be more efficient to use that 
> operation.  For cases where {{getAt(int)}} is a terminal operation (aka 
> streams) it may be the only way to go.  Or maybe using one {{iterator()}} 
> would be better overall.
> Consider the following:
> {code:groovy}
> Set<String> set = ['foo','bar','baz']
> def (foo, bar, baz) = set // inefficient; requires 3 iterators and 6 calls to 
> next
> Stream<String> stream = ['foo','bar','baz'].stream()
> def (foo, bar, baz) = stream // not possible because `getAt(int)` is terminal
> {code}



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

Reply via email to