title: Groovy/Grails: How are constraints implemented?
body: <p>In the book, <a href="http://www.infoq.com/minibooks/grails-getting-started" rel="noreferrer">Getting Started with Grails - 2e</a>, page 42 (the electronic page, and not the paper page), the following example is listed.</p>
  
  <pre><code>class Race {
  
      static constraints = {
  
          name(blank:false, maxSize:50)
  
  
          // NOTE: This doesn’t do
          // what you think it does
          startDate(min: new Date())
  
          // And this is what we're supposed
          // to be using:
          // startDate(validator: {return (it &gt; new Date())})
      }
  
      String name
      Date startDate
      // ...
  }
  </code></pre>
  
  <p>The reader is then advised to use the version of <code>startDate</code> commented out above. The reason cited is: The <code>static constraints</code> property would get evaluated only once (at server startup time), whereas our intent is to have it evaluated on each instantiation and subsequent validation of <code>Race</code>.</p>
  
  <p><strong>My question is:</strong> Why then the non-closure style of the <code>name</code> constraint works on each validation attempt but not the <code>startDate</code> constraint? And, conversely, if the closure flavor is required for <code>startDate</code>, then why is it not required for <code>name</code> as well? </p>
  
  <p>If I understand the above Groovy syntax correctly, it seems that each of the constraints listed in the <code>static constraint</code> block is, syntactically, a call to a function that takes various validation attributes as a <code>Map</code>. Now since the static block would get evaluated (once) at server startup time, both function calls too would happen (once) at the server startup time, and should lead to identical and consistent behavior in their non-closure form. <strong>Isn't it so?</strong> </p>