How can I make this work?
def getSingleton[T <: scala.Singleton]: T = {
???
}
object X
val x = getSingleton[X.type]
Or similar, appreciate the signature may need to change slightly.
How can I make this work?
def getSingleton[T <: scala.Singleton]: T = {
???
}
object X
val x = getSingleton[X.type]
Or similar, appreciate the signature may need to change slightly.
Copyright © 2021 Jogjafile Inc.
In Scala 2.13 there is built-in type class
ValueOf1 2 3In Scala 2.12 you can use type class
Witnessfrom Shapeless 1 2Or if you prefer not to depend on Shapeless you can write a macro
All the above works at compile time. If it's enough to get the value at runtime you can use Scala runtime reflection with
TypeTagor with
ClassTagor if you prefer not to depend on
scala-reflectyou can use Java reflectionIn scala, is it possible to initialise a singleton object from a TypeTag?
Get the module symbol, given I have the module class, scala macro
Get instance of singleton type in scala
Scala object import in runtime