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

Kevin Chen edited comment on GROOVY-8363 at 10/21/17 10:15 PM:
---------------------------------------------------------------

Are you sure that's quite right? Calling the delegate does involve finding its 
{{.size()}} because of the {{.collect()}}, but that shouldn't invoke a 
recursive call -- the reference there isn't to the {{getDelegate()}}; it's 
directly to the {{List}} itself.

The problem with your suggested implementation (and maybe you just didn't want 
to type this out) is that I want to be able to call methods like {{.size()}} 
and {{.collect()}} and {{.find()}}, etc. on the delegating list itself. 
Currently, I solve this issue by only removing the {{implements List<String}} 
part and retaining all of the other methods (and thus creating this ugly 
interface sort of file that's just a copy of {{List}}). If you want to check 
out the example git repository, I've also pushed a version demonstrating 
exactly that.


was (Author: aspin):
Are you sure that's quite right? Calling the delegate does involve finding its 
{{.size()}} because of the {{.collect()}}, but that shouldn't invoke a 
recursive call -- the reference there isn't to the {{getDelegate()}}; it's 
directly to the {{List}} itself.

The problem with your suggested implementation (and maybe you just didn't want 
to type this out) is that I want to be able to call methods like {{.size()}} 
and {{.collect()}} and {{.find()}}, etc. on the delegating list itself. 
Currently, I solve this issue by only remove the {{implements List<String}} 
part and retaining all of the other methods (and thus creating this ugly 
interface sort of file that's just a copy of {{List}}). If you want to check 
out the example git repository, I've also pushed a version demonstrating 
exactly that.

> 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)

Reply via email to