scala - scala 反射 API Type.dealias 有什么作用?

标签 scala scala-reflect

我有一个简单的代码,

case class Person(name: String, age:Int)
import scala.reflect.runtime.universe._
val t1 = typeOf[Person]
val t2 = t1.dealias
println(t1 == t2)

输出true,所以我想问一下Type.dealias是做什么用的?我应该什么时候使用它?一些代码示例会有帮助

我这么问是因为当我阅读 Spark 代码 ScalaReflection 时,它几乎总是在使用类型之前使用 dealias

最佳答案

类型别名看起来像

type Alias = Person

它声明了一个新类型Alias,它与Person相同。

dealias 解析这些别名,以便 typeOf[Alias].dealias 将返回 typeOf[Person]。由于 Person 不是别名,因此 typeOf[Person].dealias 不执行任何操作。

另请注意:

Type Equality can be checked with =:=. It's important to note that == should not be used to compare types for equality-- == can't check for type equality in the presence of type aliases, while =:= can.

关于scala - scala 反射 API Type.dealias 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51148684/

相关文章:

scala - Scala 反射中的去混叠类型

scala 多 sbt 项目 : object is not a member of package, 未找到类型

scala - [喷客户端] : Facebook graph API returning wrong contentype

scala - 迭代案例类数据成员

Scala 宏 - 使用 `c.prefix` 推断隐式值

scala - 如何从 scala TypeTag 获取通用简单类名?

scala - 延迟加载一些配置参数,试图在 Scala 中提出一个模式

scala - 在 Akka 中保留类型参数接收

java - 如何在 Java 代码中使用 Scala 注解

scala - 比较 Scala 反射符号