WeakTypeTag v. TypeTag

有些话、适合烂在心里 提交于 2021-01-21 04:35:07

问题


In the REPL, I'm writing out the examples from Reflection - TypeTags and Manifests.

I'm confused by the difference between WeakTypeTag and TypeTag.

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

TypeTag

scala> def paramInfo[T](x: T)(implicit tag: TypeTag[T]): Unit = {
     |   val targs = tag.tpe match {  case TypeRef(_, _, args) => args }
     |   println(s"type tag of $x has type arguments $targs")
     | }
paramInfo: [T](x: T)(implicit tag: reflect.runtime.universe.TypeTag[T])Unit

WeakTypeTag

scala> def weakParamInfo[T](x: T)(implicit tag: WeakTypeTag[T]): Unit = {
     |   val targs = tag.tpe match { case TypeRef(_, _, args) => args }
     |    println(s"type tag of $x has type arguments $targs")
     | }
weakParamInfo: [T](x: T)(implicit tag: reflect.runtime.universe.WeakTypeTag[T])Unit

Running a Simple, Non-exhaustive Example

scala> paramInfo2(List(1,2,3))
type of List(1, 2, 3) has type arguments List(Int)

scala> weakParamInfo(List(1,2,3)
     | )
type tag of List(1, 2, 3) has type arguments List(Int)

What's the difference between them?


回答1:


TypeTag guarantees that you have a concrete type (i.e. one which doesn't contain any type parameters or abstract type members); WeakTypeTag does not.

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> def foo[T] = typeTag[T]
<console>:10: error: No TypeTag available for T
       def foo[T] = typeTag[T]
                           ^

scala> def foo[T] = weakTypeTag[T]
foo: [T]=> reflect.runtime.universe.WeakTypeTag[T]

But of course it can't actually get you the generic parameters the method is called with when used like this:

scala> foo[Int]
res0: reflect.runtime.universe.WeakTypeTag[Int] = WeakTypeTag[T]

You can only build a TypeTag of a generic type if you have TypeTags for all parameters:

scala> def foo[T: TypeTag] = typeTag[List[T]]
foo: [T](implicit evidence$1: reflect.runtime.universe.TypeTag[T])reflect.runtime.universe.TypeTag[List[T]]

If you have a WeakTypeTag of a concrete type, it should behave the same as a TypeTag (as far as I know).



来源:https://stackoverflow.com/questions/29435985/weaktypetag-v-typetag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!