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

  • Home
  • SEARCH
  • 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 8904645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:11:11+00:00 2026-06-15T02:11:11+00:00

I have an anonymous class that needs to be initialized before the trait that

  • 0

I have an anonymous class that needs to be initialized before the trait that it mixes in. Early initialization won’t work because they don’t allow one to use the ‘this’ reference. I can make it work if I declare the class as an inner trait with a self type, but this seems unnecessarily verbose, as the type is only used in one place in the code and is inuitive to be inlined as an anonymous class. However, I seem to find the syntax that scala will accept and meets the initialization order requirements that I have. Here is a simplified example without the extraneous detail (assume there are reasons I’m doing things this way).

trait WaitCondition[+T] {
...
}

trait EventWaitCondition[+T] extends WaitCondition[T] {
...
}

trait Event { outer =>
    private[this] var _cachedWaitCondition : Option[WaitCondition[T]]

    def next() : WaitCondition[T] =
        //Is there a way to "inline" the defintion of NextWaitCondition
        //without screwing up the initialization order?
        _cachedWaitCondition.getOrElse{ new NextWaitCondition with EventWaitCondition[T] }

    private[this] trait NextWaitCondition { this : WaitCondition[T] =>
        outer._cache = Some(this)
        ....
    }
    ....
}

So, basically, my question is that is there a way to inline the definition of NextWaitCondition as an anonymous type without changing the initialization order between NextWaitCondition and WaitCondition (i.e., so that NextWaitCondition still initializes first)?

  • 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-15T02:11:13+00:00Added an answer on June 15, 2026 at 2:11 am

    Short answer: of course not.

    We must take your word that there is just cause for the bending the laws of init order. Ponder for a moment the souls who respect the law but still suffer under it.

    @xiefei’s answer is not kludgy; it’s sufficiently structured to effect the kludge you seek.

    There is talk about deprecating DelayedInit in favor of a postConstructor hook; but you’re really asking for a preConstructor hook, so why not just formalize that with a template method?

    And depending on the dependency between your Foo and SubFoo, this could be a matter of preferring composition to inheritance. Then there are no games with init order.

    In the following, Firstly generalizes your named solution with a template method. It has the advantage that only the use site knows or cares about it.

    trait Second {
      println("I must happen after first.")
    }
    trait SecondDelayed extends DelayedInit {
      println("SecondDelayed neutral stuff")
      def delayedInit(body: =>Unit) {
        body // body first
        println("I must be delayed after first.")
      }
    }
    trait Firstly {
      def firstly
      firstly
    }
    
    object Test extends App {
      def trial(t: =>Unit) {
        println("----")
        t
      }
      // candidate for least obnoxious
      trial {
        new Firstly with Second {
          def firstly {
            println("Do this firstly.")
          }
          println("Don't care when this happens.")
        }
      }
      trial {
        // current solution
        new Something with Second
        trait Something { this: Second =>
          println("First code.")
        }
      }
      trial {
        // prefer anon
        new Second {
          println("Anon first?") // nope
        }
      }
      trial {
        // DelayedInit solution
        new SecondDelayed {
          println("Anon first, then other delayed.")
        }
      }
      trial {
        // the "delayed" code must be idempotent,
        // or find a useful way to trigger execution;
        // here, the delayed code happens twice.
        class Foo extends SecondDelayed {
          println("Foo wants init, too")
        }
        new Foo {
          println("Anon first, then other delayed.")
        }
      }
      /* early defs are only for defs, with no this
      new {
        println("Anon first.")
      } with Second
      */
      trial {
        // trait code doesn't participate
        new DelayedInit with Second {
          def delayedInit(body: =>Unit) {
            println("My crucial early init business")
            body
          }
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that needs to provide a fast classification service. For example,
I have a class that uses 5.3 feature anonymous function ( https://github.com/JeffreyWay/Easy-WordPress-Custom-Post-Types/blob/master/jw_custom_posts.php ), but
I have a javascript function (class) that takes a function reference as one paremter.
As bash doesn't have first class functions, I'm simulating anonymous functions by passing a
I have a class that creates a List<Action<int>> and holds on to them until
I have simple class but with anonymous block of code. I need to cover
I have a Linq query that returns an anonymous type, which I then use
This is for a school project. I have a Query<E> class that holds an
I have a simple Screen class in C# that has a bunch of events
I have a pure virtual class CF that denotes any continuous function in 1D

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.