title: groovy closure parameters
body: <p>The following example of using the sendMail method provided by the grails mail plugin appears in <a href="https://rads.stackoverflow.com/amzn/click/com/1590599950" rel="noreferrer" rel="nofollow noreferrer">this book</a>.</p>
  
  <pre><code>sendMail {
      to "foo@example.org"
      subject "Registration Complete"
      body view:"/foo/bar", model:[user:new User()]
  }
  </code></pre>
  
  <p>I understand that the code within {} is a closure that is passed to sendMail as a parameter. I also understand that <code>to</code>, <code>subject</code> and <code>body</code> are method calls.</p>
  
  <p>I'm trying to figure out what the code that implements the sendMail method would look like, and my best guess is something like this:</p>
  
  <pre><code>MailService {
  
      String subject
      String recipient
      String view
      def model
  
      sendMail(closure) {
          closure.call()
          // Code to send the mail now that all the 
          // various properties have been set
      }
  
      to(recipient) {
          this.recipient = recipient
      }
  
      subject(subject) {
          this.subject = subject;
      }
  
      body(view, model) {
          this.view = view
          this.model = model
      }
  }
  </code></pre>
  
  <p>Is this reasonable, or am I missing something? In particular, are the methods invokedwithin the closure (<code>to</code>, <code>subject</code>, <code>body</code>), necessarily members of the same class as <code>sendMail</code>?</p>
  
  <p>Thanks,
  Don</p>