我已经制定了我自己的特点和课程框架,扩展了我的特点。所有类的父类都是一种名为“契约”的特征。‘'Combinator’和'ElementaryContract‘是合同的两个直接子代。
def disintegrateContract[T](element: T): Elem =
{
element match
{
case com <: Combinator => matchCombinator(com)
case e <:ElementaryContract =>matchElementaryContract(e)
}
}我想要创建一个match类,识别传递的‘契约’是'Combinator‘还是'ElementaryContract’的子类型,然后将它传递给其他函数。这是我得到的编译错误:
'=>' expected but '<:' found可能它不识别子类型操作符。我怎样才能做到这一点?
发布于 2013-12-05 15:25:26
如果正确理解,通常的模式匹配应该是可以的--如果Bar扩展Foo,则是Foo (加上其他内容):
class SuperA
class ImplA extends SuperA
class SuperB
class ImplB extends SuperB
def disintegrateContract[T](element: T) = element match {
case a: SuperA => println("I'm ancestor of Super A")
case b: SuperB => println("I'm ancestor of Super B")
}
disintegrateContract(new ImplA)
// I'm ancestor of Super A
disintegrateContract(new ImplB)
// I'm ancestor of Super B确切地说,您的情况应该是超级的,SuperA和SuperB将对其进行扩展,但没有任何改变。
https://stackoverflow.com/questions/20403701
复制相似问题