David Silva
David Silva

Reputation: 2017

Optional parameter in Partial Function?

My team develop kind of "core" project, which is being used by a couple of our other projects. It is the reason, why we dont want to change keenly some method signatures.

Let's assume we have method like this:

override def calculateNextStep: PartialFunction[(Option[OrderType], Option[OrderState]), OrderType] = {
    case (Some(OrderTypeX), Some(InProgress))  => SomeOrderTypeA
    case (Some(OrderTypeY), Some(Done)) => SomeOrderTypeB
    case (None, None) => SomeOrderTypeC
}

But now I need to override this method in one of "non core" projects - I need add one extra parameter - let say parentOrder. It is necessary to calculate next step properly. But I wouldn't like to affect other projects. It would be great to add new optional parameter (with default value None) and dont affect other projects. Is any way to to achieve this goal?

Upvotes: 0

Views: 157

Answers (1)

Mario Galic
Mario Galic

Reputation: 48420

We cannot change method signature when overriding. Try overloading instead like so

def calculateNextStep(a: Option[OrderType], b: Option[OrderState] c: Option[ParentOrder]): OrderType = 
  (a, b, c) match {
    case (Some(OrderTypeX), Some(InProgress), Some(aParentOrder)) => // do something
    case _ => calculateNextStep(a, b)
  }

Upvotes: 2

Related Questions