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

  • Home
  • SEARCH
  • 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 6157067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:43:19+00:00 2026-05-23T20:43:19+00:00

I need help with one of the Groovy 1.8 DSL features. Consider this test

  • 0

I need help with one of the Groovy 1.8 DSL features.

Consider this test case:

/**
 * For odd number of elements, treat the last element as a call to a getter
 *
 * case            a b  c
 * equivalent      a(b).getC()
 */
void testTrailingElementAsGetter() {
    def drank = false
    def more = 'more'
    def drink = { String s -> [milk: { drank = true }()] }

    def d = drink more milk

    assert drank
}

If I change [milk: { drank = true }()] to [foo: { drank = true }()], the test case still passes. Could this be a bug in the new implementation or am I missing something in the Groovy syntax?

EDIT – SOLVED: Both @han and @Gareth Davis posted the right clues. Here are some more details to understand this test:

    groovy:000> more = 'more'
    ===> more 
    groovy:000> drank = false 
    ===> false
    groovy:000> drink = { String s -> [milk: { drank = true }()] }
    ===> groovysh_evaluate$_run_closure1@20c87621
[A] groovy:000> drink more
    ===> {milk=true}    
[B] groovy:000> drank  
    ===> true          
    groovy:000> drink more milk
    ===> true
    groovy:000> drink more water
    ===> null 

Line [A] returns a map as @han pointed out. In line [B], drank is already true, due to the closure executing immediately after creation (as @Gareth Davis pointed out), similar to the JavaScript module pattern. I don’t think this “test” is the best way to showcase this feature – the side-effect on drank is mis-leading.

  • 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-05-23T20:43:20+00:00Added an answer on May 23, 2026 at 8:43 pm
    groovy:000> drank =false
    ===> false
    groovy:000> d = {x -> [y: {drank=true}()]}
    ===> groovysh_evaluate$_run_closure1@170b819
    groovy:000> drank
    ===> false
    groovy:000> d 2  
    ===> {y=true}
    groovy:000> drank
    ===> true
    

    x on first invoke sets drank to true then becomes just a map {y : true}

    quite a puzzling behaviour really

    from this we can see
    there are many ways to synthesize a DSL from the new 1.8 syntax
    – key-dereferencing maps
    – closure and call
    – class and method

    be bary

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help with one ajax function This is raw page setup. Page will
I really need help on this one. I am having a simple login form
I need your help with this one again. I want to send special characters
We Really need some help on this one: We've Struggled with all the Apple
Okay, I need help. I'm usually pretty good at SQL queries but this one
I need a help for one thing : this is my jQuery idea: var
I need some help with this one.... I have this simple model: public class
I need help on both the visual and the programmatic end of this one.
i need help redirecting this two links to one link: Link #1: http://www.domain.com/index.php?til=d_news&id_new=1 Link
I need help. I have HTML code like this, <div class=close>Close</div> <table class=table><tr><td>One</td><td>Two</td></tr></table> <div

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.