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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:45:46+00:00 2026-06-18T09:45:46+00:00

I’ve added a method to Closure ‘s metaClass , but I don’t seem to

  • 0

I’ve added a method to Closure‘s metaClass, but I don’t seem to be able to get a reference for the instance the method is being called on. In this example, delegate is set to the script instance, not the f closure i’m invoking fixedPoint on:

Closure.metaClass.fixedPoint = {
    while (it != (it = delegate.call(it))) {}
    it
}
def f = { Math.round(it / 2.0) }
println f.fixedPoint(9)

gives

Caught: groovy.lang.MissingMethodException: No signature of method: test.call() is applicable for argument types: (java.lang.Integer) values: [9]

What a I doing wrong here?

  • 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-18T09:45:48+00:00Added an answer on June 18, 2026 at 9:45 am

    Explanation:
    If you do Closure.metaClass.fixedPoint = ... then the class Closure will get a new MetaClass, which is ExpandoMetaClass. And in a next step the method is added. Now the default meta class for a Closure is ClosureMetaClass, which neither allows you to add methods, nor does it care about other meta classes much. When you do def f = { Math.round(it / 2.0) } then you actually create a new class (and instance of it). It extends Closure, but is not Closure itself. And this class will have by default ClosureMetaClass as meta class, totally ignoring what you did with the meta class of Closure.

    Solution:
    You have to force the usage of ExpandoMetaClass, thus the first line of your code (before f is assigned) should be ExpandoMetaClass.enableGlobally():

    ExpandoMetaClass.enableGlobally()
    Closure.metaClass.fixedPoint = {     
      while (it != (it = delegate.call(it))) {}     
      it
    }
    
    def f = { Math.round(it / 2.0) } 
    assert f.fixedPoint(9) == 1
    

    At least for me this code runs without exception…

    Side Note:
    The Closure stored in the meta class to form the method is usually a clone of what you gave in. The meta class will then set the delegate on the copy. Inspecting the delegate of f by for example println f.@delegate won’t show you the result then.

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I want to construct a data frame in an Rcpp function, but when I
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.