简体   繁体   English

Scala:在内部类型上获取TypeTag

[英]Scala: Getting a TypeTag on an inner type

I'm trying to get a TypeTag or ClassTag for an inner type to provide a ClassTag to a method with type parameters. 我正在尝试为内部类型获取TypeTagClassTag ,以便为具有类型参数的方法提供ClassTag Specicfically, I have 具体来说,我有

trait Baz
trait Foo { type Bar <: Baz }

and I'd like to do something like 我想做点什么

import scala.reflect.runtime.universe._
import scala.reflect._
typeTag[Foo#Bar] // or maybe
classTag[Foo#Bar]

but I get a 'no typetag available' error. 但我得到一个'没有打字标签'的错误。 My ultimate goal is to provide ClassTag s to something like this 我的最终目标是将ClassTag提供给这样的东西

import scala.reflect.ClassTag
class Doer[A,B]()(implicit ctA:ClassTag[A], ctB:ClassTag[B])
object Fooer extends Doer[Foo, Foo#Bar]()(classTag[Foo], classTag[Foo#Bar])

Normally you can get type tags and class tags from inner types just fine. 通常,您可以从内部类型中获取类型标记和类标记。 However, Foo#Bar is an abstract type. 但是, Foo#Bar是一种抽象类型。 Neither type tags nor class tags can be acquired from abstract types. 无法从抽象类型中获取类型标记和类标记。 You can get a weak type tag instead: 您可以改为获取弱类型标签:

scala> weakTypeTag[Foo#Bar]
res1: reflect.runtime.universe.WeakTypeTag[Foo#Bar] = WeakTypeTag[Foo#Bar]

scala> class Doer[A, B]()(implicit wttA: WeakTypeTag[A], wttB: WeakTypeTag[B])
defined class Doer

scala> new Doer[Foo, Foo#Bar]()
res2: Doer[Foo,Foo#Bar] = Doer@1994551a

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

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