SkyWalker
SkyWalker

Reputation: 14309

Scala type mismatch on default parameter type

I define a Scalatest helper to compare two Saddle frames like this:

def compareFrames[RX, CX](result: Frame[RX, CX, Double], expected: Frame[_, _, Double], tol: Double = 1e-10): Unit = {
   // TODO: implement
   ???
}

But now I'd like to add a set of Column Index elements that I'd like to skip for testing e.g. Matlab has apparently a different formula to compute Skewness than Saddle ... note that the Set element type has to be the same as of the Frame column index type:

def compareFrames[RX, CX](result: Frame[RX, CX, Double], expected: Frame[_, _, Double], toSkip: Set[CX] = Set(), tol: Double = 1e-10): Unit = {
   // TODO: implement
   ???
}

but this leads to the following compiler error when the function is invoked without specifying the default parameters:

Error:(53, 7) type mismatch;
 found   : scala.collection.immutable.Set[Nothing]
 required: Set[String]
Note: Nothing <: String, but trait Set is invariant in type A.
You may wish to investigate a wildcard type such as `_ <: String`. (SLS 3.2.10)
Error occurred in an application involving default arguments.
      compareFrames(result, expected)

Upvotes: 0

Views: 269

Answers (1)

Federico Pellegatta
Federico Pellegatta

Reputation: 4017

Your default parameter Set() for toSkip is of type Set[Nothing], you have to ask for a Set[CX]() of type Set[CX]:

def compareFrames[RX, CX](result: Frame[RX, CX, Double], expected: Frame[_, _, Double], toSkip: Set[CX] = Set[CX](), tol: Double = 1e-10): Unit = {
   // TODO: implement
   ???
}

Upvotes: 1

Related Questions