Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:41:25+00:00 2026-06-04T07:41:25+00:00

I’am reading ‘Groovy in Action’ recently. In chapter 7, it introduced the *. operator

  • 0

I’am reading ‘Groovy in Action’ recently. In chapter 7, it introduced the *. operator . When i run the code about this operator, i get some mistakes.

class Invoice {                                          
    List    items                                        
    Date    date                                         
}                                                        
class LineItem {                                         
    Product product                                      
    int     count                                        
    int total() {                                        
        return product.dollar * count                    
    }                                                    
}                                                        
class Product {                                          
    String  name                                         
    def     dollar                                       
}                                                        

def ulcDate = new Date(107,0,1)
def ulc = new Product(dollar:1499, name:'ULC')           
def ve  = new Product(dollar:499,  name:'Visual Editor') 

def invoices = [                                         
    new Invoice(date:ulcDate, items: [                   
        new LineItem(count:5, product:ulc),              
        new LineItem(count:1, product:ve)                
    ]),                                                  
    new Invoice(date:[107,1,2], items: [                 
        new LineItem(count:4, product:ve)                
    ])                                                   
]                                                        

//error
assert [5*1499, 499, 4*499] == invoices.items*.total()  

The last line will throw a exception.
At first, i can explain why this error happend. The invocies is a List, and the element’s type is Invoice. So directly using items will make a error. I attempt to fix it by using invoices.collect{it.items*.total()}

But still get a fail assert. So, how can i make the assert success and why invoices*.items*.total() will throw a exception.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:41:25+00:00Added an answer on June 4, 2026 at 7:41 am

    The result of invoices*. operator is a list, so the result of invoices*.items is a list of lists. flatten() can be applied to a lists and returns a flat list, so you can use it to make a list of LineItems from your list of list of ListItems. You can then apply total() to its elements using the spread operator:

    assert [5*1499, 499, 4*499] == invoices*.items.flatten()*.total()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.