简体   繁体   English

TypeTag和比较运算符?

[英]TypeTag and comparison operators?

I don't really understand why you can't work with a TypeTag object itself, can someone explain why the below code doesn't work? 我真的不明白为什么您不能使用TypeTag对象本身,有人可以解释为什么下面的代码不起作用吗? I'm also asking why the TypeTag object doesn't directly support operators like =:= . 我还问为什么TypeTag对象不直接支持=:=类的运算符。 I know this has something to do with the typeOf[T] function, but frustratingly I can't seem to grab this directly either (example myTT.typeOf ) 我知道这与typeOf[T]函数有关,但令人沮丧的是我似乎也无法直接抓住它(例如myTT.typeOf

Thanks 谢谢

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
    }
  }
}

You need the tpe method: 您需要使用tpe方法:

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

(You also need another set of parentheses because of the left-associativity of + ) (由于+的左相关性,您还需要另一组括号)

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

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