Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force initialization of Scala singleton object

Tags:

object

scala

I'm working on an automatic mapping framework built on top of Dozer. I won't go into specifics as it's not relevant to the question but in general it's supposed to allow easy transformation from class A to class B. I'd like to register the projections from a class's companion object.

Below is a (simplified) example of how I want this to work, and a Specs test that assures that the projection is being registered properly.

Unfortunately, this doesn't work. From what I can gather, this is because nothing initializes the A companion object. And indeed, if I call any method on the A object (like the commented-out hashCode call, the projection is being registered correctly.

My question is - how can I cause the A object to be initialized automatically, as soon as the JVM starts? I don't mind extending a Trait or something, if necessary.

Thanks.

class A {
  var data: String = _
}

class B {
  var data: String = _
}

object A {
  projekt[A].to[B]

}

"dozer projektor" should {
  "transform a simple bean" in {
//    A.hashCode
      val a = new A
      a.data = "text"
      val b = a.-->[B]

      b.data must_== a.data
  }
}
like image 985
Electric Monk Avatar asked Jun 06 '11 08:06

Electric Monk


People also ask

What is the easiest way to implement singleton in Scala?

Singleton objects effectively implement the singleton pattern in Scala with no additional effort. As shown in the previous example, they can contain both mutable and immutable states as well as methods. However, if any mutable state is defined, singletons are not thread-safe by default.

How do you define a singleton object in Scala?

A singleton object is created by using object keyword. The method in the singleton object is globally accessible. You are not allowed to create an instance of singleton object.

When we are writing a Scala program we always start with object instead of using a class unlike Java What is that type of object is?

Singleton Objects Scala is more object-oriented than Java because in Scala, we cannot have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance, i.e., Object. You create singleton using the keyword object instead of class keyword.


2 Answers

Short answer: You can't. Scala objects are lazy, and are not initialized until first reference. You could reference the object, but then you need a way of ensuring the executing code gets executed, reducing the problem back to the original problem.

like image 59
Dave Griffith Avatar answered Oct 21 '22 22:10

Dave Griffith


In ended up doing this:

trait ProjektionAware with DelayedInit
{
  private val initCode = new ListBuffer[() => Unit]

  override def delayedInit(body: => Unit)
  {
    initCode += (() => body)
  }


  def registerProjektions()
  {
    for (proc <- initCode) proc()
  }
}

object A extends ProjektionAware {
  projekt[A].to[B]

}

Now I can use a classpath scanning library to initialize all instances of ProjektionAware on application bootstrap. Not ideal, but works for me.

like image 31
Electric Monk Avatar answered Oct 21 '22 22:10

Electric Monk