groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (GROOVY-8363) Implementing List with a delegated data source results in StackOverflowException
Date Sun, 22 Oct 2017 00:32:00 GMT

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

Paul King commented on GROOVY-8363:
-----------------------------------

Perhaps I misunderstood. Isn't this what you wanted:
{code}
import groovy.transform.CompileStatic

class DelegateList {
    private List<String> lowerCaseStrings = []
    private List<String> upperCaseStrings = new DelegatingListImplementation()

    private abstract static class DelegatingList {
        @Delegate
        abstract List<String> getDelegate()
    }

    @CompileStatic
    private class DelegatingListImplementation extends DelegatingList {
        @Override
        List<String> getDelegate() {
            lowerCaseStrings*.toUpperCase()
        }
    }

    DelegateList(Collection<String> strings) {
        lowerCaseStrings = strings*.toLowerCase()
    }

    List<String> getUppercase() {
        return upperCaseStrings
    }

    static void main(args) {
        def d = new DelegateList(['hello', 'bye'])
        assert !d.uppercase.isEmpty()
        assert d.uppercase.contains('BYE')
        assert d.uppercase.find{ it.size() < 5 } == 'BYE'
        assert d.uppercase.size() == 2
    }
}
{code}

> Implementing List with a delegated data source results in StackOverflowException
> --------------------------------------------------------------------------------
>
>                 Key: GROOVY-8363
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8363
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Kevin Chen
>
> Preface: not experienced with submitting bugs to the Groovy project, so please bear with
me if anything's unclear.
> I'm getting a StackOverflowException from this bit of code (I think just including this
snippet demonstrates better than I can explain verbally):
> {code:title=DelegateList.groovy|borderStyle=solid}
> class DelegateList {
>     private List<String> lowerCaseStrings = []
>     private DelegatingListImplementation uppercaseStrings = new DelegatingListImplementation()
>     private abstract static class DelegatingList implements List<String> {
>         abstract List<String> getDelegate()
>         @Override
>         int size() {
>             return delegate.size()
>         }
>         @Override
>         boolean isEmpty() {
>             return delegate.isEmpty()
>         }
>         // etc.
>     }
>     private class DelegatingListImplementation extends DelegatingList {
>         @Override
>         List<String> getDelegate() {
>             return lowerCaseStrings.collect { it.toUpperCase() }
>         }
>     }
>     DelegateList(Collection<String> strings) {
>         lowerCaseStrings = strings.toList().collect { it.toLowerCase() }
>     }
>     List<String> getUppercase() {
>         return uppercaseStrings
>     }
>     public static void main(String[] args) {
>         def d = new DelegateList(['hello', 'bye'])
>         println d.getUppercase() // StackOverflowError
>     }
> }
> {code}
> The equivalent doesn't happen in Java. Example repository with both runnables is here:
[https://github.com/aspin/groovy-delegate-list]. I'm not really sure how to get deeper into
the source of this (Groovy's closure resolving strategies?) and/or how to work around.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message