我正在尋找一種方法來將一個斯卡拉單例對象作爲一個字符串(例如:package1.Main)給予實際的主要實例,以便我可以調用它的方法。問題的斯卡拉反映字符串單身對象
例子:
package x {
object Main extends App {
val objectPath: String = io.StdIn.readLine("Give an object: ") // user enters: x.B
// how to convert the objectPath (String) to a variable that references singleton B?
val b1: A = magicallyConvert1(objectPath)
b1.hi()
val b2: B.type = magicallyConvert2(objectPath)
b2.extra()
}
trait A {
def hi() = {}
}
object B extends A {
def extra() = {}
}
}
如何才能magicallyConvert1和magicallyConvert2功能來實現?