在asInstanceOf中引用嵌套类

时间:2015-03-26 18:49:42

标签: scala casting inner-classes nested-class

我正在not found: value Duck

    class Type
    class Value(val t: Type)
    class Duck extends Type {
        class Val extends Value(this)
    }
    def f(individual: Value) = individual.t match {
        // case t: Duck => individual.asInstanceOf[Value] //this is ok
         case t: Duck => individual.asInstanceOf[Duck.Val] //but I need this
    }

在此添加一些细节以改善质量问题。正式的质量检查不能错。如果更多的字母改善了你的问题,那一定是这样的。现在,我的问题要好得多,可以发布。

1 个答案:

答案 0 :(得分:4)

您可能正在寻找:

def f(individual: Value) = individual.t match {
     case t: Duck => individual.asInstanceOf[t.Val] 
}

或者这个:

def f(individual: Value) = individual.t match {
     case t: Duck => individual.asInstanceOf[Duck#Val]
}

在Scala中,为外部类的每个实例定义内部类型。因此,您要查找的类型是t.Val,因为您必须提供外部类实例以完全了解内部类类型。如果你想获得所有这些内部类型的超类型,你可以Outer#Inner

相关问题