Reputation: 16755
I wrote following in Scala REPL. I do not understand what REPL has created for me. Is c
object of class Any
? How can I find to which class c
belongs to?
scala> object c
defined object c
Upvotes: 0
Views: 320
Reputation: 81
The class (type) of object c
is c.type
. By declaring an object in Scala you implicitly define both class and singleton object (Scala will generate those for you). Since class is not explicitly defined in source code, special object member called type
exist to refer to object's class.
Upvotes: 0
Reputation: 1646
By declaring object c
, you basically instantiate a singleton object with name c
, that has no custom properties or methods. This object is not of type Any
, and you can see its class by:
scala> object c
defined object c
scala> c.getClass
res14: Class[_ <: c.type] = class c$
To understand more about singleton objects, read the reference here. An excerpt:
Singleton objects are sort of a shorthand for defining a single-use class, which can’t directly be instantiated, and a val member at the point of definition of the object, with the same name. Indeed, like vals, singleton objects can be defined as members of a trait or class, though this is atypical.
Upvotes: 2