繁体   English   中英

如何从案例类别值中获取类别类型

[英]How to obtain class type from case class value

我有以下代码片段:

case class Test(i:Int)

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

有没有办法从具有Test类型的值aTest.type

libraryDependencies += scalaOrganization.value % "scala-reflect" % scalaVersion.valuebuild.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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM