简体   繁体   English

为什么嵌套实例化中没有TypeTag(由scala代码运行符解释)?

[英]Why there is no TypeTag available in nested instantiations (when interpreted by scala code runner)?

I am trying to modify the behavior of List.toString according to its type parameter. 我试图根据其类型参数修改List.toString的行为。 Since List can't be extended, it is wrapped by a custom class CList (could be with implicits, but the problem would remain the same?). 由于List无法扩展,它被自定义类CList包装(可能带有隐含,但问题会保持不变?)。 The problem arises when printing a CList of CList s. 在打印CListCList时会出现问题。 Below are examples and respective output in comments: 以下是评论中的示例和相应输出:

object Foo {
  import scala.reflect.runtime.universe._

  class CList[A: TypeTag](val l: List[A]) {
    override def toString = typeOf[A] match {
      case t if t =:= typeOf[Char] => l.mkString
      case _ => "[" + l.mkString(", ") + "]"
    }
  }
}

import Foo.CList

val c = new CList(List(1, 2)) // prints "[1, 2]"
println(c)

val c2 = new CList(List('a', 'b')) // prints "ab"
println(c2)

val c3 = new CList(List(
   List(1, 2),
   List(3, 4)))

println(c3) // prints "[List(1, 2), List(3, 4)]"

val c4 = new CList(List(
   new CList(List(1, 2)),
   new CList(List(3, 4))))
println(c4) // prints "No TypeTag available for this.Foo.C[Int]"

I was able to reduce the code to: 我能够将代码减少到:

import scala.reflect.runtime.universe.TypeTag
class A
implicitly[TypeTag[A]]

When it is run with scala interpreter, it gives an error No TypeTag available for this.A . 当它使用scala解释器运行时,它会给出一个错误No TypeTag available for this.A Looking at code produced by the interpreter, I came up with code that compiler can't handle: 看看解释器生成的代码,我想出了编译器无法处理的代码:

class Main {
  class A
  def main(args: Array[String]) {
    class B
    implicitly[TypeTag[A]] // ok
    implicitly[TypeTag[B]] // error
  }
}

So it seems, that the compiler can't generate type tags for classes defined inside methods. 看来,编译器无法为方法中定义的类生成类型标记。 Running with -Xlog-implicits complains cannot create a TypeTag referring to local class Main.B: use WeakTypeTag instead . 使用-Xlog-implicits运行抱怨cannot create a TypeTag referring to local class Main.B: use WeakTypeTag instead

Works for me, scala 2.10.2, the output is: 适合我,scala 2.10.2,输出是:

[1, 2]
ab
[List(1, 2), List(3, 4)]
[[1, 2], [3, 4]]

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

相关问题 在Scala中,如何在TypeTag可用时转换值 - In Scala, how to cast a value when a TypeTag is available Scala-没有TypeTag可用当使用案例类尝试获取TypeTag时发生异常吗? - Scala - No TypeTag Available Exception when using case class to try to get TypeTag? Scala反射-为什么选择TypeTag和Type? - Scala reflection - why TypeTag and Type? 没有 TypeTag 可用于案例 class 使用 scala 3 和火花 3 - No TypeTag available for a case class using scala 3 with spark 3 在Scala中,如何创建TypeTag [Map [A​​,B]],而只有TypeTag [A]和TypeTag [B]可用? - In Scala, How to create TypeTag[Map[A,B]], while only TypeTag[A] and TypeTag[B] are available? 带有scala2.10.6的spark1.6.2没有可用的TypeTag - spark1.6.2 with scala2.10.6 No TypeTag available Scala编译器在使用泛型的方法中说“没有TypeTag可用于T” - Scala compiler says “No TypeTag available for T” in method using generics Spark Scala API:官方示例中spark.createDataFrame中没有typeTag - Spark Scala API: No typeTag available in spark.createDataFrame in official example 具有“No TypeTag available”的Scala / Spark应用程序“def main”样式App中的错误 - Scala/Spark App with “No TypeTag available” Error in “def main” style App 隐式参数vs TypeTag何时使用以及为什么 - implicit paramters vs TypeTag when to use and why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM