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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:15:21+00:00 2026-05-31T00:15:21+00:00

I have a class that I’m trying to make extend DelayedInit : class Foo

  • 0

I have a class that I’m trying to make extend DelayedInit:

class Foo extends DelayedInit {
  // expensive initialisation code
}

However when I try to run sbt compile I get the error:

Foo needs to be abstract, since method delayedInit in trait DelayedInit of type (x: => Unit)Unit is not defined

My understanding is that by extending the DelayedInit trait any initialisation code should automatically be wrapped in a closure and run in the delayedInit method after initialisation is complete. However I’ve had a stab at googling and can’t seem to find an example of usage. What am I missing?

  • 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-31T00:15:22+00:00Added an answer on May 31, 2026 at 12:15 am

    How DelayedInit Works and its Sample Usage

    DelayedInit trait provides the ability to control at which point initialisation code within a class or an object (but not a trait) is run.

    Any initialisation code within classes or objects (but not traits) that are inherited from DelayedInit is passed by compiler during the initialisation to the delayedInit method and then it’s up to you when you want to run it.

    delayedInit method is called automatically as part of the initialisation and running the code that is passed as a parameter straight away within the method is still running the code during the initialisation.

    Let’s start with a basic scenario:

    object Main extends DelayedInit  {
    
       println ("  initialisation of Main object")
       override def delayedInit (body: => Unit) {
         println("delayedInit")
         body
       }
    
       def main (args: Array[String]) {
         println("main method") 
       }
    }
    

    Will print:

    delayedInit
      initialisation of Main object
    main method
    

    In fact, delayedInit method will be called once for every class that inherits the trait within the class hierarchy. A slightly more complicated scenario:

    abstract class MyApplication extends DelayedInit {
       println ("  initialisation of MyApplication class")
    }
    
    object Main extends MyApplication  {
    
       println ("  initialisation of Main object")
       override def delayedInit (body: => Unit) {
         println("delayedInit")
         body
       }
    
       def main (args: Array[String]) {
         println("main method") 
       }
    }
    

    Will print:

    delayedInit
      initialisation of MyApplication class
    delayedInit
      initialisation of Main object
    main method
    

    Since main method is the first method to run once the initialisation is over, what we really want to do is to save all the initialisation code passed to delayedInit and run it later, probably from within main, since there is potentially more than one bit of code we could conveniently store it in a ListBuffer (we need to keep appending to preserve the natural execution order). The code inside ‘Main` object could look something like this:

    private val init = new scala.collection.mutable.ListBuffer[()=>Unit]
    override def delayedInit (body: => Unit) {
      println("delayedInit")
      init += (()=>body) // will result in NullPointerException
    }
    
    
    def main (args: Array[String]) {
      println("main method") 
      for (code <- init) code ()
    }
    

    However, there is a catch 22: because initialisation of init field is delayed along with every other initialisation statement there isn’t any ListBuffer[()=>Unit] object to preserve the initialisation code for later use!

    But, remember?

    Any initialisation code within classes or objects (but not traits)
    that are inherited from DelayedInit is passed by compiler during the
    initialisation to the delayedInit method…

    Let’s re-shuffle things a bit, move functionality that memorises the code for later use into StoredInit trait that inherits directly from DelayedInit:

    trait StoredInit extends DelayedInit {
       println ("initialisation of StoredInit trait")
       private val init = new scala.collection.mutable.ListBuffer[()=>Unit]
       override def delayedInit (body: => Unit) {
         println("delayedInit")
         init += (()=>body)
       }   
       def initialise () {
         for (code <- init) code ()     
       }
    }
    
    // extend StoredInit instead of DelayedInit
    abstract class MyApplication extends StoredInit {
       println ("  initialisation of MyApplication class")
    }
    
    object Main extends MyApplication  {
    
       println ("  initialisation of Main object")
    
       def main (args: Array[String]) {
         println("main method") 
         initialise() // finally perform the delayed initialisation
       }
    }
    

    Will print:

    initialisation of StoredInit trait
    delayedInit
    delayedInit
    main method
      initialisation of MyApplication class
      initialisation of Main object
    

    Finally, why doesn’t DelayedInit trait include a default implementation of delayedInit method?

    To keep the compiler hook de-coupled from the actual implementation of delayed initialisation behaviour. The desired behaviour will be different for console application and server-side component that has to work within a specific container.

    trait App, however, inherits from DelayedInit and provides default implementation for Scala applications.

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

Sidebar

Related Questions

I have class that extends another class, that extend another class.. and so on.
I have class that extends another class. class TWITTER_FOLLOWERS extends TWITTER_BOT in TWITTER_FOLLOWERS i
I have class that extends AsyncTask class and I create instance of this class
I have class foo that contains a std::auto_ptr member that I would like to
I have class that extends 'IntentService' - this class occasionally receives data from a
i have class that extends thread and in one of its methodes i added
I have class that extends BroadcastReceiver and gets all new sms. When I get
I have class that extends View. private class Draw2D extends View{ public Draw2D(Context context)
I have class that extends Activity. That has a Logout menu options. When I
I have class (MyCustomWebView) that extend webview can i do something like this ?

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.