简体   繁体   中英

TypeTags of type aliases with an upper type bound

I'm trying to get the TypeTag for a type alias with an upper type bound in a library class. The (simplified) library code looks like:

abstract class A {
  trait B
  type T <: B
  def newT: T
}
object A {
  def apply() = new A {
    class B2 extends B
    type T = B2
    def newT = new B2
  }
}
class E[T: TypeTag](x: T)

On the REPL I'm doing:

val a = A()
val t = a.newT
val e = new E(t)

And I get back "No TypeTag available for aT"

You can think of class A as some container (eg Map) and of the apply method in the companion object as creating the actual implementation (eg HashMap). B would be like MapEntry in this analogy and I want to construct put this MapEntry into another container that asks for the TypeTag of the given element.

I hope the idea is clear. Is there a way to do this?

I'm using Scala 2.10.4.

You example actually works when you define the subclass of A explicitly, like this:

object A {
  class Ab extends A {
    class B2 extends B
    type T = B2
    def newT = new B2
  }
  def apply() = new Ab
}

Alternatively you could use a WeakTypeTag, like this:

class E[T: WeakTypeTag](x: T)

For details see also this issue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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