2

I have made my own framework of traits and classes that extend my traits. The parent of all classes is a trait named 'Contract'. 'Combinator' and 'ElementaryContract' are two immediate children of Contract.

def disintegrateContract[T](element: T): Elem = 
{

    element match
    {
      case com <: Combinator =>  matchCombinator(com)
      case e <:ElementaryContract =>matchElementaryContract(e)
    }
}

I want to make a match class that recognizes whether a passed 'Contract' is a subtype of 'Combinator' or 'ElementaryContract' and then pass it to some other functions. This is the compilation error I get:

 '=>' expected but '<:' found

Probably it does not recognize the subtype operator. How can I make this happen?

1 Answer 1

3

If understood you correctly the usual pattern matching should be fine -- if Bar extends Foo, it is Foo (plus something else):

class SuperA
class ImplA extends SuperA

class SuperB
class ImplB extends SuperB

def disintegrateContract[T](element: T) = element match {
   case a: SuperA =>  println("I extend SuperA")
   case b: SuperB =>  println("I extend SuperB")
}

disintegrateContract(new ImplA)
// I extend SuperA
disintegrateContract(new ImplB)
// I extend SuperB

To have exact your situation there should be Super which SuperA and SuperB will extend, but it changes nothing.

Sign up to request clarification or add additional context in comments.

2 Comments

So, you are saying I do not need the <: operator?
@Core_Dumped yes, you don't need it and no, AFAIK, bounds are not supported in pattern matching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.