|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Spread-dot and collect() questionHi,
I read from the groovy docs (http://groovy.codehaus.org/Operators#Operators-SpreadOperator%28.%29 ): "The spread operator may be used a method call or property access, and returns a list of the items returned from each child call. So you may effectively override the spread operator by implementing a custom collect method." I tried overriding the collect method on my collection, but it is never called: class ExpressionNodeList extends ArrayList { ExpressionNodeList(List l) { super(l) } List collect(Closure c) { println "In our custom collect" return new ExpressionNodeList( xxxx.collect(c) ) } } When I do something like: assertTrue root.Chapters instanceof ExpressionNodeList assertTrue root.Chapters*.Chapter instanceof ExpressionNodeList This second line fails, and I get no printlns. The docs imply that root.Chapters.collect { it.Chapter } will be called, but it seems not. What's wrong - the impl or the docs... or my brain? FYI - Groovy 1.6.4 Thanks Marc --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Spread-dot and collect() questionOn 3 Nov 2009, at 12:19, Marc Palmer wrote: > Hi, > > I read from the groovy docs (http://groovy.codehaus.org/Operators#Operators-SpreadOperator%28.%29 > ): > > "The spread operator may be used a method call or property access, > and returns a list of the items returned from each child call. So > you may effectively override the spread operator by implementing a > custom collect method." > > I tried overriding the collect method on my collection, but it is > never called: Here's a concise complete example: class CustomList extends ArrayList { List collect(Closure c) { println "Custom collect!" return new LinkedList( this.collect(c) ) } } def x = new CustomList() x << [name:'A'] x << [name:'B'] assert x instanceof CustomList assert x.size() == 2 assert x.name == ['A', 'B'] assert x*.name instanceof LinkedList No printlns, and the last assert fails. Where is the problem? Thanks Marc ~ ~ ~ Marc Palmer Blog > http://www.anyware.co.uk Twitter > http://twitter.com/wangjammer5 Grails Rocks > http://www.grailsrocks.com --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Spread-dot and collect() questionThe docs are a lie[1]. I've never been able to overload *., either.
[1] http://www.urbandictionary.com/define.php?term=the+cake+is+a+lie ~~ Robert Fischer, Smokejumper IT Consulting. Enfranchised Mind Blog http://EnfranchisedMind.com/blog Grails Expert Retainer Services http://smokejumperit.com/grails-retainer/ Marc Palmer wrote: > > On 3 Nov 2009, at 12:19, Marc Palmer wrote: > >> Hi, >> >> I read from the groovy docs >> (http://groovy.codehaus.org/Operators#Operators-SpreadOperator%28.%29): >> >> "The spread operator may be used a method call or property access, and >> returns a list of the items returned from each child call. So you may >> effectively override the spread operator by implementing a custom >> collect method." >> >> I tried overriding the collect method on my collection, but it is >> never called: > > Here's a concise complete example: > > class CustomList extends ArrayList { > List collect(Closure c) { > println "Custom collect!" > return new LinkedList( this.collect(c) ) > } > } > > def x = new CustomList() > x << [name:'A'] > x << [name:'B'] > > assert x instanceof CustomList > assert x.size() == 2 > assert x.name == ['A', 'B'] > assert x*.name instanceof LinkedList > > No printlns, and the last assert fails. > > Where is the problem? > > Thanks > Marc > ~ ~ ~ > Marc Palmer > Blog > http://www.anyware.co.uk > Twitter > http://twitter.com/wangjammer5 > Grails Rocks > http://www.grailsrocks.com > > > > > > > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Spread-dot and collect() questionYes, I suspect that the docs are out of date. The code implementing the spreading is very similar to the code implementing collect but currently collect isn't explicitly called. Perhaps it once did call collect and was changed to tweak some of the edge cases. A browse through svn history may shed some light. Perhaps it could be made to call collect again - it would need some further investigation - if you have what you believe to be a very good use case I would create a feature enhancement Jira. We should have a doco fix Jira as well in the meantime. You could override iterator() instead for some scenarios. Paul. Robert Fischer wrote: > The docs are a lie[1]. I've never been able to overload *., either. > > [1] http://www.urbandictionary.com/define.php?term=the+cake+is+a+lie > > ~~ Robert Fischer, Smokejumper IT Consulting. > Enfranchised Mind Blog http://EnfranchisedMind.com/blog > > Grails Expert Retainer Services > http://smokejumperit.com/grails-retainer/ > > > Marc Palmer wrote: >> >> On 3 Nov 2009, at 12:19, Marc Palmer wrote: >> >>> Hi, >>> >>> I read from the groovy docs >>> (http://groovy.codehaus.org/Operators#Operators-SpreadOperator%28.%29): >>> >>> "The spread operator may be used a method call or property access, >>> and returns a list of the items returned from each child call. So you >>> may effectively override the spread operator by implementing a custom >>> collect method." >>> >>> I tried overriding the collect method on my collection, but it is >>> never called: >> >> Here's a concise complete example: >> >> class CustomList extends ArrayList { >> List collect(Closure c) { >> println "Custom collect!" >> return new LinkedList( this.collect(c) ) >> } >> } >> >> def x = new CustomList() >> x << [name:'A'] >> x << [name:'B'] >> >> assert x instanceof CustomList >> assert x.size() == 2 >> assert x.name == ['A', 'B'] >> assert x*.name instanceof LinkedList >> >> No printlns, and the last assert fails. >> >> Where is the problem? >> >> Thanks >> Marc >> ~ ~ ~ >> Marc Palmer >> Blog > http://www.anyware.co.uk >> Twitter > http://twitter.com/wangjammer5 >> Grails Rocks > http://www.grailsrocks.com >> >> >> >> >> >> >> >> --------------------------------------------------------------------- >> To unsubscribe from this list, please visit: >> >> http://xircles.codehaus.org/manage_email >> >> >> > > --------------------------------------------------------------------- > To unsubscribe from this list, please visit: > > http://xircles.codehaus.org/manage_email > > > > --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Free embeddable forum powered by Nabble | Forum Help |