Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refer to an uninstantiated class in Scala?

Tags:

types

class

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 the value of the_class directly. I appear to be able to 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 don't appear to be able to bind the variable to any value. Is this 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
                   ^
like image 259
Dan Barowy Avatar asked Dec 04 '25 10:12

Dan Barowy


1 Answers

Did you mean:

val the_class = classOf[FooBar]
like image 134
Tomasz Nurkiewicz Avatar answered Dec 06 '25 01:12

Tomasz Nurkiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!