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 7586733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:29:58+00:00 2026-05-30T19:29:58+00:00

I have some noob questions about delegate in groovy meta programming. with this line

  • 0

I have some noob questions about delegate in groovy meta programming.

with this line of code

grailsApplication.domainClasses.each { gdc ->
 def domClass = gdc.clazz
 domClass.metaClass.invokeMethod{name,args ->
 //some line of code
 def result = invokeMethod(delegate,args)
 }
}

what is the content of delegate, name and args in here ?
In general, what is delegate and what kind contents does it has ?

Any help and explanation will be appreciated

  • 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-30T19:29:59+00:00Added an answer on May 30, 2026 at 7:29 pm

    You have a typo; domClass.metaClass.invokeMethod{name,args -> should be domClass.metaClass.invokeMethod = { String name,args ->. This is assigning a Closure to be the handler for all method calls on that class. Since you’re handling method calls, you’ll need to know the name of the method and the method arguments, so those are the parameters of the closure. The args parameter will be an Object[] array containing the args from the method invocation. So for example domClass.foo() will have name “foo” and an empty args array, domClass.foo("purple") will have a 1-element array containing the String “purple”, etc.

    Think of delegate as this inside the closure. this is actually the class instance that the Closure is defined in, not the closure itself or the object that the method is being called on. Since you’ll often need the object, delegate points to it. Ordinarily the delegate will be the containing instance where the closure is defined, so method calls are resolved by looking there, and if not found, a missing method exception is thrown. But you can set the delegate to be another handler which has the methods that are being called, and it will be delegated to instead. This is particularly useful with Groovy builders and DSLs.

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

Sidebar

Related Questions

I have some code like this in a winforms app I was writing to
I apologize in advance, I am a PHP noob! I have form with some
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some classes layed out like this class A { public virtual void
I have some C# / asp.net code I inherited which has a textbox which
This might be a noob question, but can't find an answer anywhere. I have
Typical noob question. :-) I looked at some of similar questions raised here, the
hey just a couple quick noob questions about writing my first ios app. Ive
Another jQuery noob question - what am I doing wrong?? I have some HTML
Sorry if this is a noob question, but I have been given the dubious

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.