I want to get the instance of a singleton type in Scala, is this possible?
Example: (I know it can be done easier in this case)
sealed trait Animal
case object Dog extends Animal
case object Cat extends Animal
trait Person {
def name: String
// Is there a "FavoriteAnimal.instance" syntax?
def mostImportantThings = (FavoriteAnimal.instance, name)
protected type FavoriteAnimal <: Animal with scala.Singleton
}
case class DogPerson(override val name: String) extends Person {
override type FavoriteAnimal = Dog.type
}
case class CatPerson(override val name: String) extends Person {
override type FavoriteAnimal = Cat.type
}
scala.Singleton?objects and literals (see docs.scala-lang.org/sips/42.type.html).def onlySingleton(arg: Singleton): String = arg.toString: scastie.scala-lang.org/kU481gG3Rtih0rt6dEF2ag.classfile. Don't remember which at the moment. But I don't know any which are as undocumented.