2014-12-21 4 views
3

내부 유형에 TypeTag 또는 ClassTag을 입력하여 유형 매개 변수가있는 메소드에 ClassTag을 제공하려고합니다. Specicfically, 나는스칼라 : 내부 유형에 TypeTag 가져 오기

trait Baz 
trait Foo { type Bar <: Baz } 

을하고 난

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

처럼 뭔가를하고 싶지만 나는 '더 typetag 가능한'오류가 발생하지 않습니다. 나의 궁극적 인 목표는 일반적으로 당신이 잘 내부 유형에서 유형 태그와 클래스 태그를 얻을 수있는이

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]) 

답변

5

처럼 뭔가 ClassTag의를 제공하는 것입니다. 그러나 Foo#Bar은 추상 형식입니다. 형식 태그 나 클래스 태그는 추상 형식에서 가져올 수 없습니다. 대신 약한 유형의 태그를 사용할 수 있습니다.

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] = [email protected] 
관련 문제