2

タイプタグがタイプエイリアスで機能しないのはなぜですか。与えられた例

trait Foo
object Bar {
  def apply[A](implicit tpe: reflect.runtime.universe.TypeTag[A]): Bar[A] = ???
}
trait Bar[A]

次のメソッド内でエイリアスを使用したいと思います。これは、A約20回入力する必要があるためです。

def test {
  type A = Foo
  implicit val fooTpe = reflect.runtime.universe.typeOf[A] // no funciona
  Bar[A]                                                   // no funciona
}

次の試み:

def test {
  type A = Foo
  implicit val fooTpe = reflect.runtime.universe.typeOf[Foo] // ok
  Bar[A]                                                     // no funciona
}

そのため、エイリアスをまったく使用できないようです。

4

2 に答える 2

1

代わりにweakTypeOfを使用してください。リフレクションは、グローバルに表示される宣言とローカルの宣言を内部的に区別するため、それらも異なる方法で処理する必要があります。この疣贅は、Scalaの新しいバージョンで削除される可能性があります。

于 2013-02-05T10:40:34.900 に答える
0

def apply宣言の変更:

import scala.reflect.runtime.universe._
trait Foo
object Bar {
  def apply[A]()(implicit tpe: TypeTag[A]): Bar[A] = ???
}
trait Bar[A]
class test {
  type A = Foo
  implicit val foo = typeOf[A]
  def test = Bar[A]()                                                 
}
于 2013-02-03T23:36:23.707 に答える