DelayedInit or OnCreate, any solution?


DelayedInit is deprecated in favor a long-pending OnCreate:

However, the ticket was recently closed (along with all DelayedInit tickets), which begs the question: what now?


I don’t have a solution yet. Before checking what one could/should do, it would be good to have some use cases. Where do people use DelayedInit (other than for App)?


The other one I know of is ScalaFX and the scala.application.JFXApp. Clearly, it is a similar usage to App, but I have a feeling that there might be differences in the details. JavaFX has some quirks in how applications are started that might cause JFXApp to be a bit different than App.


There’s one use of DelayedInit in ScalaTest:



Specs2 has at least one, and it has caused me trouble a couple times. It tends to get new users who implement Scopes with abstract classes rather than traits, and get a surprise double-call or unexpected order.

A couple examples of confused users:


Forgive me if this is a stupid idea, but maybe we can use implicits for this? If we have a special typeclass:

trait OnCreate[T : Classtag] {
  def apply(t : T) : T 
class Foo
trait FooInit extends OnCreate[Foo] {
  def apply(t : T) : T = ???
object FooInit {
  implicit def ev : FooInit = new FooInit {}

We only need the compiler to translate
val foo = new Foo{}
val fooInit = implicitly[FooInit]; val foo = fooInit(new Foo{})

By using implicit we can also avoid mutating Foo, but just create an initialized copy of it.


Well I use it (as well as others) for DSL purposes to populate name tags for specific objects by using Java runtime reflection. E.g:

trait Foo {
  val m1 : MySpecialType
  val m2 : MySpecialType

I want to tag the m1 and m2 objects with their names "m1" and "m2" respectively.


Could that use case be satisfied by sourcecode.Name from


Very cool lib. I’ll try it out!