DelayedInit or OnCreate, any solution?


#1

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?


#2

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)?


#3

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.


#4

There’s one use of DelayedInit in ScalaTest:

http://doc.scalatest.org/3.0.0/index.html#org.scalatest.fixture.NoArg

Bill


#5

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:



#6

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{}
into
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.


#7

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.


#8

Could that use case be satisfied by sourcecode.Name from https://github.com/lihaoyi/sourcecode?


#9

Very cool lib. I’ll try it out!