mardi 28 août 2018

How to access a Scala object instance given it's full qualified name?

So I have defined a Scala object using the following code:

trait SomeTrait {
  def someMethod(): Unit
}

package somepackage1 {
  object Impl1 extends SomeTrait {
    def someMethod(): Unit = { }
  }
  object Impl2 extends SomeTrait {
    def someMethod(): Unit = { }
  }
}

I want to access the object given its fully qualified name i.e. somepackage1.Impl2. A method something like following:

package object somewhereElse {
  def getImpl(qualifiedName: String): SomeTrait = {
    ???
  }
}

What should be the code as part of getImpl method?





Aucun commentaire:

Enregistrer un commentaire