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

时间:2012-07-01 18:31:50

标签: scala types

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

我想做这样的事情:

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
             ^

最后一次

2 个答案:

答案 0 :(得分: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并没有真正以任何干净的方式对类型进行建模,所以如果你想在某种意义上“获得类型”,你可能会运气不好。

答案 1 :(得分: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
相关问题