简体   繁体   中英

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 eg 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)

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
   ???
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM