3

说我有

trait Foo[T] { def bar: Bar[T] }

并希望bar在 a 上调​​用时Foo[Int](即Bar[Int]在这种情况下)从类型标记Foo[Int]和名称中获取返回类型"bar"(我们可以假设没有重载,或者我们可以将它们区分开来)。这可以用 scala-reflect 完成吗?

4

2 回答 2

4

这是否接近您想要的(使用asSeenFrom)?

val tt = typeTag[Foo[Int]]

scala> tt.tpe
    .members
    .find(_.name.toString == "bar").get
    .asMethod
    .returnType
    .asSeenFrom(tt.tpe, tt.tpe.typeSymbol)

res68: reflect.runtime.universe.Type = Bar[Int]

我当然把类型安全抛到了窗外。稍微好一些:

scala> tt.tpe
     .members
     .find(_.name.toString == "bar")
     .filter(_.isMethod)
     .map(_.asMethod.returnType.asSeenFrom(tt.tpe, tt.tpe.typeSymbol))

res74: Option[reflect.runtime.universe.Type] = Some(Bar[Int])
于 2015-03-25T14:29:12.180 回答
2

万一将来有人遇到这种情况:有了这种变化

trait Foo[T] { def bar: Option[T] }
trait Bar[T] extends Foo[T]

val tt = typeTag[Bar[Int]]

我不得不稍微改变@mz 的回答:

val m = tt.tpe
  .member(newTermName("bar"))
  .asMethod

m.returnType
  .asSeenFrom(tt.tpe, m.owner)
于 2015-04-01T07:34:50.300 回答