If I have a object as follows:
case object Foo
and I try to create a value like this
Either[Foo, B]
I get a compile error saying that Foo cannot be found. But if I do this:
Either[Foo.type, B]
It compiles. My question is if this correct to do this?
Whenever you want to declare the type of an object
in Scala you have to declare it like YourObject.type
.
The reason is simple, as YourObject
is already the instance. So .type
is the way you have to declare the Type of an Object (Singleton) in Scala.
Here an example:
object YourObject
def doit(obj: YourObject.type) = {}
def doitEventually(obj: Option[YourObject.type]) = {}
doit(YourObject)
doitEventually(Some(YourObject))
I couldn't find the according documentation, so maybe someone can help out with that.
The Specification is here: singleton-types (as mentioned by Mojo in the comments)