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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:53:43+00:00 2026-06-03T09:53:43+00:00

I have not understood the following code snippet why afterDelay(0) {…} , a locally

  • 0

I have not understood the following code snippet why afterDelay(0) {...}, a locally defined function can be stored into agenda? Can someone help me understand the afterDelay(0) {...} in the run function?

abstract class Simulation {

  type Action = () => Unit

  case class WorkItem(time: Int, action: Action)

  private var curtime = 0
  def currentTime: Int = curtime

  private var agenda: List[WorkItem] = List()

  private def insert(ag: List[WorkItem], item: WorkItem): List[WorkItem] = {
    if (ag.isEmpty || item.time < ag.head.time) item :: ag
    else ag.head :: insert(ag.tail, item)
  }

  def afterDelay(delay: Int)(block: => Unit) {
    val item = WorkItem(currentTime + delay, () => block)
    agenda = insert(agenda, item)
  }

  private def next() {
    (agenda: @unchecked) match {
      case item :: rest => 
        agenda = rest 
        curtime = item.time
        item.action()
    }
  }

  def run() {
    afterDelay(0) {
      println("*** simulation started, time = "+
          currentTime +" ***")
    }
    while (!agenda.isEmpty) next()
  }
}
  • 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-03T09:53:45+00:00Added an answer on June 3, 2026 at 9:53 am
    afterDelay(0) {
        println(...)
    }
    

    Is equivalent to the following:

    afterDelay(0)({
        println(...)
    })
    

    The function afterDelay is invoked a new WorkItem (item) is added to the list, not the function itself. The parameter block: => Unit is a “By-Name Parameter” (see the Scala Language Specification section 4.6.1): the expression used as the argument is implicitly converted into a “parameterless method” (without being evaluated first) that will be invoked whenever the variable inside the method is accessed (no () required).

    In this case that is when the function resulting from () => block is invoked: it is invoked at item.action() which occurs at some point after the new WorkItem is added to the list (and afterDelay returns).

    If it was written as (taking in a function paramater, not a by-name/thunk):

    def afterDelay(delay: Int)(block: () => Unit) {   // take a function
      // new function will invoke function named by "block" when invoked ...
      val item = WorkItem(..., () => block())
      // or skip wrapping the function in a function ...
      // val item = WorkItem(..., block)
      ...
    }
    

    Then it would need to be invoked passing in a function:

    afterDelay(0)(() => { // need a function
        println(...)
    })
    

    Or, alternative syntax, still a function of () => Unit, but the outside parenthesis can be avoided:

    afterDelay(0) { () => // need a function
        println(...)
    } 
    

    Extract from the SLS, 4.6.1 By-Name Parameters:

    The type of a value parameter may be prefixed by =>, e.g. x: => T. The type of such a parameter is then the parameterless method type => T. This indicates that the corresponding argument is not evaluated at the point of function application, but instead is evaluated at each use within the function. That is, the argument is evaluated using call-by-name.

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

Sidebar

Related Questions

I have the following code wich is raising an EConvertError - can not assign
I have come across the following jQuery code but could not understand it. What
I have the following code snippet: // Notify the source (the other control). if
My problem is a little bit complex. I have the following code: $(document).ready(function() {
Saw the following code snippet and I have problems to understand how it works.
I have to work through some code and found the following function, which I
I do not understand the following example, let's say I have these functions: #
I not sure I have understood all the principles of nosql, especially Cassandra project,
Consider the following code: function Robot(weapon) { this.weapon = weapon; this.fire = function() {
Reviewing a quite old project I found the following curious code snippet (only relevant

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.