title: Groovy 1.8 a b c style
body: <p>I need help with one of the Groovy 1.8 DSL features.</p>
  
  <p>Consider <a href="http://svn.codehaus.org/groovy/trunk/groovy/groovy-core/src/test/gls/syntax/Gep3Test.groovy" rel="nofollow">this</a> test case:</p>
  
  <pre><code>/**
   * 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 -&gt; [milk: { drank = true }()] }
  
      def d = drink more milk
  
      assert drank
  }
  </code></pre>
  
  <p>If I change <code>[milk: { drank = true }()]</code> to <code>[foo: { drank = true }()]</code>, the test case still passes. Could this be a bug in the new implementation or am I missing something in the Groovy syntax?</p>
  
  <p><strong>EDIT - SOLVED</strong>: Both @han and @Gareth Davis posted the right clues. Here are some more details to understand this test:</p>
  
  <pre><code>    groovy:000&gt; more = 'more'
      ===&gt; more 
      groovy:000&gt; drank = false 
      ===&gt; false
      groovy:000&gt; drink = { String s -&gt; [milk: { drank = true }()] }
      ===&gt; groovysh_evaluate$_run_closure1@20c87621
  [A] groovy:000&gt; drink more
      ===&gt; {milk=true}    
  [B] groovy:000&gt; drank  
      ===&gt; true          
      groovy:000&gt; drink more milk
      ===&gt; true
      groovy:000&gt; drink more water
      ===&gt; null 
  </code></pre>
  
  <p>Line <code>[A]</code> returns a map as @han pointed out. In line <code>[B]</code>, <code>drank</code> is already <code>true</code>, due to the closure executing immediately after creation (as @Gareth Davis pointed out), similar to the <a href="http://www.yuiblog.com/blog/2007/06/12/module-pattern/" rel="nofollow">JavaScript module pattern</a>. I don't think this "test" is the best way to showcase this feature - the side-effect on <code>drank</code> is mis-leading. </p>