То, что вы можете делать следующее в Scala, довольно изящно:
scala> class FooBar
defined class FooBar
scala> val a = new FooBar
a: FooBar = FooBar@7efeedca
scala> val the_class = a.getClass
the_class: java.lang.Class[_ <: FooBar] = class FooBar
scala> val b = the_class.newInstance
b: FooBar = FooBar@1ef1df56
Предположим, я хочу установить значение the_class
напрямую.Кажется, я могу объявить переменную правильного типа:
scala> var the_class: java.lang.Class[_ <: FooBar] = null
the_class: java.lang.Class[_ <: FooBar] = null
Но я не могу связать переменную с любым значением.Это возможно?
scala> the_class = class FooBar
<console>:1: error: illegal start of simple expression
the_class = class FooBar
^
scala> the_class = FooBar
<console>:9: error: not found: value FooBar
the_class = FooBar
^