You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2022/06/26 23:23:00 UTC
[jira] [Comment Edited] (GROOVY-10666) Implement multiple-assignment (aka destructuring) via getAt(IntRange) when supported
[ https://issues.apache.org/jira/browse/GROOVY-10666?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17558932#comment-17558932 ]
Eric Milles edited comment on GROOVY-10666 at 6/26/22 11:22 PM:
----------------------------------------------------------------
{code:groovy}
class MySet {
@Delegate Set<String> strings = []
String getAt(int index) {
println("getAt($index)")
DefaultGroovyMethods.getAt(this, index)
}
Iterator<String> iterator() {
println "iterator()"
def iterator = strings.iterator()
new Iterator<String>() {
@Override
boolean hasNext() {
println "next()"
iterator.hasNext()
}
@Override
String next() {
iterator.next()
}
}
}
}
Set<String> strings = new MySet()
strings << 'foo'
strings << 'bar'
strings << 'baz'
def (foo, bar, baz) = strings
{code}
Outputs:
{code}
getAt(0)
iterator()
next()
getAt(1)
iterator()
next()
next()
getAt(2)
iterator()
next()
next()
next()
{code}
was (Author: emilles):
{code:groovy}
class MySet {
@Delegate Set<String> strings = []
Iterator<String> iterator() {
println "iterator()"
def iterator = strings.iterator()
new Iterator<String>() {
@Override
boolean hasNext() {
println "next()"
iterator.hasNext()
}
@Override
String next() {
iterator.next()
}
}
}
}
Set<String> strings = new MySet()
strings << 'foo'
strings << 'bar'
strings << 'baz'
def (foo, bar, baz) = strings
{code}
Outputs:
{code}
iterator()
next()
iterator()
next()
next()
iterator()
next()
next()
next()
{code}
> Implement multiple-assignment (aka destructuring) via getAt(IntRange) when supported
> ------------------------------------------------------------------------------------
>
> Key: GROOVY-10666
> URL: https://issues.apache.org/jira/browse/GROOVY-10666
> Project: Groovy
> Issue Type: Improvement
> Components: 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.
> 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.7#820007)