How can I refer to an uninformed class in Scala?

The fact that you can do the following in Scala is pretty neat:

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


Suppose I want to set a value directly the_class

. It seems I can declare a variable of the correct type:

scala> var the_class: java.lang.Class[_ <: FooBar] = null
the_class: java.lang.Class[_ <: FooBar] = null


But I cannot bind the variable to any value. Is it possible?

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



source to share

1 answer

You meant:

val the_class = classOf[FooBar]




All Articles