I have the following code snippet:
case class Test(i:Int)
val b = Test // b: Test.type
val a = Test(1) // a: Test
Is there a way to get from value a
which has a Test
type to Test.type
?
I have the following code snippet:
case class Test(i:Int)
val b = Test // b: Test.type
val a = Test(1) // a: Test
Is there a way to get from value a
which has a Test
type to Test.type
?
Add libraryDependencies += scalaOrganization.value % "scala-reflect" % scalaVersion.value
to build.sbt
and then you can try
import scala.reflect.runtime.universe.{Type, TypeTag, typeOf}
case class Test(i:Int)
val b = Test
val a = Test(1)
def getType[A: TypeTag](a: A): Type = typeOf[A]
getType(a) // Test
getType(a).companion // Test.type
getType(b) // Test.type
getType(b).companion // Test