kups (3) [Avatar] Offline
#1
I cannot get Listing_7_23_GPath.groovy to run in groovyConsole with Groovy 1.6 or 1.5.7.

I tried entering on my own and then copying the source code downloaded from the website. I saw one other post on this, but nothing about an error. I thought maybe it was a version 1.6 diff, but I get the same error under 1.5.7

The error I get is on line 31 of the original script (below)
assert [5*1499, 499, 4*499] == invoices.items*.total() //#3

Since invoices is an ArrayList of invoices, it fails to apply the total() method via the spread operator on items*. since the invoices object doesn't have an items property.

I'm fairly new to Groovy and made it work with individual invoices... This works for one of the invoices in the ArrayList:

assert [5*1499, 499] == invoices[0].items*.total()

but that's not really the spirit of the example.

Thoughts?
Mittie (397) [Avatar] Offline
#2
Re: Listing_7_23_GPath.groovy does not run under Groovy 1.6, 1.5.7
Hi Gary,

doh - I think you have even hit a Groovy regression! I will investigate whether there is an open issue for this.

Meanwhile, you can use the flatten() method to avoid nested lists. The assertions in Listing 7.23 then become

assert [5*1499, 499, 4*499] == invoices.items.flatten()*.total()
assert ['ULC'] == invoices.items.flatten().grep{it.total() > 7000}.product.name
def searchDates = invoices.grep{it.items.flatten().any{it.product == ulc}}.date*.toString()

thanks a lot for posting!
Dierk
Mittie (397) [Avatar] Offline
#3
Re: Listing_7_23_GPath.groovy does not run under Groovy 1.6, 1.5.7
Mittie (397) [Avatar] Offline
#4
Re: Listing_7_23_GPath.groovy does not run under Groovy 1.6, 1.5.7
Mittie (397) [Avatar] Offline
#5
Re: Listing_7_23_GPath.groovy does not run under Groovy 1.6, 1.5.7
Issue resolved.
It's not a regression in Groovy but a deliberate change.
Silly me.
kups (3) [Avatar] Offline
#6
Re: Listing_7_23_GPath.groovy does not run under Groovy 1.6, 1.5.7
I should have posted this before, but thanks for looking into it.