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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:50:49+00:00 2026-06-02T00:50:49+00:00

How to avoid closure when passing a function as a parameter to a class

  • 0

How to avoid closure when passing a function as a parameter to a class constructor?

My class constructor accepts a function of type () => Unit. During the program working process, I want to access a key-value pair from a mutable map, that was not defined at the moment of creating the object with the constructor

val cats = scala.collection.mutable.Map[String, Cat]

class Trigger(period: Long, f: () => Unit) {

  //pseudocode:
  when period passes, f()

}

someWonderfulObject += new Trigger (1000, () => cats("Hershy").meow)

cats += ("Hershy" -> Cat())

and then, when the wonderful object triggers its triggers, I get the error that there is no key like “Hershy”. I conclude that apparently, this is due to the f passed to the Trigger cons being closured in a state when there was no “Hershy”.

My question is now – how to avoid Scala making use of closures here and instead look at the actual state of cats?

UPDATE:

The code for “when period passes” is this:

def update(tpf: Float) {
  timePassed += tpf
  if(timePassed > period) f()
}

The tpf value comes from above, so here all this seems ok.

UPDATE:

I have found the problem to be in another, totally irrelevant place, I have confused the containers of all this stuff. Howevr, thank you all, I have learned something from the answers.

  • 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-02T00:50:50+00:00Added an answer on June 2, 2026 at 12:50 am

    Closures do no such thing (that would be quite hard to specify, have terrible performance, and make the whole thing useless). The closure may contains a reference to cats, or to the instance containing cats, not a copy.

    This code works as expected :

    val cats = collection.mutable.Map[String, String]()
    
    class Trigger(name: String, f: () => Unit) {
      def fire = {
        println("Fire " + name)
        f()
      }
    }
    
    val trigger = new Trigger("calling Hershy", () => println(cats("Hershy")))
    cats += "Hershy" -> "meow"
    trigger.fire // prints Fire calling Hershy
    

    The closure is not at fault, I suspect this is something that does not appear in your sample.

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

Sidebar

Related Questions

To avoid the long class path issue in windows, i modified my build.xml like[1]
How can I avoid that a user starts the same program twice? The current
Because I want to avoid repetitive code, and I'm using a lot of text
I want to write a function that would return the boolean true if the
I'm trying to group some exisiting top-level functions inside a closure (to avoid polluting
This is mostly an out-of-curiosity question. Consider the following functions var closure ; function
How can I tell closure compiler that an anonymous function should not be removed
I am trying to annotate my Javascript properly in order to avoid Google Closure
I have a method that takes an anonymous function parameter. This function's parameter is
To avoid crashing anything I specify the version number for every gem in my

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.