Scala 如何从case类值中获取类类型

Scala 如何从case类值中获取类类型,scala,Scala,我有以下代码片段: case class Test(i:Int) val b = Test // b: Test.type val a = Test(1) // a: Test 有没有一种方法可以从具有Test类型的valuea获取到Test.type?添加libraryDependencies+=scalaOrganization.value%”scala将“%scalaversation.value反映到build.sbt,然后您可以尝试 import scala.reflect.run

我有以下代码片段:

case class Test(i:Int)

val b = Test // b: Test.type
val a = Test(1) // a: Test

有没有一种方法可以从具有
Test
类型的value
a
获取到
Test.type

添加
libraryDependencies+=scalaOrganization.value%”scala将“%scalaversation.value
反映到
build.sbt
,然后您可以尝试

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

libraryDependencies+=scalaOrganization.value%”scala反映“%scalaversation.value
build.sbt
,然后您可以尝试

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

Test的可能重复。type
Test
类的伴生对象的类型,而不是
a
类的类型。你到底为什么想要这个?您可以分享您的用例吗?
Test的可能副本。type
Test
类的伴生对象的类型,而不是
a
类的类型。你到底为什么想要这个?你能分享你的用例吗?