0

创建对象后是否可以获取类型字段的类型?

我想做这样的事情:

scala> class A { type T = String }
defined class A

scala> val a = new A
a: A = A@591171

scala> a.T   
<console>:13: error: value T is not a member of A
           a.T
             ^

最后

4

2 回答 2

5

你想对类型做什么?您可以通过类的类型(没有实例)以各种方式使用类型投影:

scala> class A { type T = String }
defined class A

scala> val x: A#T = "test"
x: java.lang.String = test

scala> def f(b: A#T) = b
f: (a: java.lang.String)java.lang.String

或者,如果您启用-Ydependent-method-types,您可以从实例中获取类型成员:

scala> val a = new A
a: A = A@6a3de2df

scala> val x: a.T = "test"
x: a.T = test

scala> def f(b: a.T) = b
f: (b: a.T)a.T

Scala 在 2.10 之前的反射 API 并没有真正以任何干净的方式对类型进行建模,所以如果你想从其他意义上“获取类型”,你可能不走运。

于 2012-07-01T19:22:47.350 回答
4

一种方法是反射(从 2.10M4 开始):

// with static types
scala> class A { type T = String }
defined class A

scala> import reflect.runtime.{universe => u}
import reflect.runtime.{universe=>u}

scala> val t = u.typeOf[A]
t: reflect.runtime.universe.Type = A

scala> val types = t.declarations.filter(_.isType)
types: Iterable[reflect.runtime.universe.Symbol] = SynchronizedOps(type T)

scala> types.toList.head.typeSignature
res9: reflect.runtime.universe.Type = String

// with instances
scala> val a = new A
a: A = A@68d7c870

scala> import reflect.runtime.{currentMirror => m}
import reflect.runtime.{currentMirror=>m}

scala> m.reflect(a).symbol.asType // same type as t
res20: reflect.runtime.universe.Type = A
于 2012-07-01T19:11:39.507 回答