Specifying type of a Case Object in Either

后端 未结 1 826
自闭症患者
自闭症患者 2021-01-21 10:05

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 c

1条回答
  •  迷失自我
    2021-01-21 10:43

    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)

    0 讨论(0)
提交回复
热议问题