简体   繁体   中英

Scala: typeTag for an abstract type?

I want to do something like the following:

trait Sim {
    type Probe
    var probe: Probe

    def reflectProbe() {
      // works: 
      val tt_int = scala.reflect.runtime.universe.typeTag[Int]
      // does not work (No TypeTag available for Sim.this.Probe):
      val tt_probe = scala.reflect.runtime.universe.typeTag[Probe]
    }
}

class WorldProbe

class World extends Sim {
  type Probe = WorldProbe
  var probe: Probe = new WorldProbe
}

I want to write a reflection method in trait sim that reflects the probe class of trait sim, defined as an abstract type Probe. How can I make typetag information for the abstract type available?

Thanks!

对于抽象类型,可以使用WeakTypeTags

val tt_probe = scala.reflect.runtime.universe.weakTypeTag[Probe]

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