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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:42:58+00:00 2026-06-14T21:42:58+00:00

In Groovy, I can make an object invokable like a function by monkey-patching the

  • 0

In Groovy, I can make an object invokable like a function by monkey-patching the metaclass’ call method:

myObject.metaClass.call = { "hello world" }
println myObject() // prints "hello world"

patching call only allows me to invoke the object with no arguments. Is there a way of allowing objects to be invoked with arguments using standard function-like syntax?


edit: one answer is exactly as tim_yates suggests, although it’s worth noting from ataylor’s comment that you can simply override call without explicit metaprogramming:

class MyType {
    def call(...args) {
        "args were: $args"
    }
}

def myObject = new MyType()
println myObject("foo", "bar") // prints 'args were ["foo", "bar"]'

Apparently the trick is the variadic signature using ...args.

  • 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-06-14T21:43:00+00:00Added an answer on June 14, 2026 at 9:43 pm

    You could do:

    myObject.metaClass.call = { ...args -> "hello $args" }
    assert myObject( 'one', 'two', 'three' ) == 'hello [one, two, three]'
    

    (as you can see, args is an array of Objects)

    Or for one parameter:

    myObject.metaClass.call = { who -> "hello $who" }
    

    Or if you want that single parameter as an optional param, you could do:

    myObject.metaClass.call = { who = null -> "hello ${who ?: 'world'}" }
    
    assert myObject( 'tim' ) == 'hello tim'
    assert myObject() == 'hello world'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know i can make use of sort() function in Groovy to sort List.
I know in Groovy you can invoke a method on a class/object using a
I'm using the jQuery UI Autocomplete function. I can make it work with the
Where can I get the Groovy bundle for TextMate? I checked the TextMate subversion
Can someone please explain the major differences between Scala, Groovy and Clojure. I know
I'm writing a Groovy script which uses third party java code that I can't
I am hoping to use Groovy more as a functional language than I can
I am trying to replace a large string in groovy. But can't get it
I've created a domain class in Grails like this: class MyObject { static hasMany
On a Mac with JDK and all Groovy necessities installed, how can Groovy files

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.