Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 类型标记和比较运算符?_Scala_Reflection - Fatal编程技术网

Scala 类型标记和比较运算符?

Scala 类型标记和比较运算符?,scala,reflection,Scala,Reflection,我真的不明白为什么不能使用TypeTag对象本身,有人能解释一下为什么下面的代码不起作用吗?我还想问为什么TypeTag对象不直接支持像=:=这样的操作符。我知道这与typeOf[T]函数有关,但令人沮丧的是,我似乎也无法直接抓住这一点(例如myTT.typeOf) 谢谢 import scala.reflect.runtime.universe._ object TestRun extends App { class Matcher[T:TypeTag] { def test[

我真的不明白为什么不能使用
TypeTag
对象本身,有人能解释一下为什么下面的代码不起作用吗?我还想问为什么
TypeTag
对象不直接支持像
=:=
这样的操作符。我知道这与
typeOf[T]
函数有关,但令人沮丧的是,我似乎也无法直接抓住这一点(例如
myTT.typeOf

谢谢

import scala.reflect.runtime.universe._

object TestRun extends App {

  class Matcher[T:TypeTag] {
    def test[U](x: U)(implicit tag: TypeTag[U]) = {
      val myTT = implicitly[TypeTag[T]]

      println("=:=" + myTT =:= tag) //error
    }
  }
}

您需要
tpe
方法:

println("=:=" + (myTT.tpe =:= tag.tpe))

(由于
+
的左关联性,您还需要另一组括号)

谢谢,您是否了解tpe对象与TypeTag本身的区别?如果我们立即使用tpe,我不明白typetag类的意义是什么