A class of mine is a generic and defines iv as MHashMap[DateTime, T]. MHashMap is an alias for scala.collection.mutableHashMap. Then I use this code to expose a minimum finding functionality
def minimum: BigDecimal = {
iv match {
case dtMcM: MHashMap[DateTime, MyCustomClass] => dtMcM.minBy(_._2.bdField)._2.bdField
case dtBdM: MHashMap[DateTime, BigDecimal] => dtBdM.minBy(_._2)._2
case dtDlM: MHashMap[DateTime, Double] => dtDlM.minBy(_._2)._2.toBigDecimal
case dtItM: MHashMap[DateTime, Int] => dtItM.minBy(_._2)._2
case _ => throw new IllegalArgumentException("Unsupported underlying type")
}
}
But the complier complains:
No implicit Ordering defined for T with Int.
case dtItM: MHashMap[DateTime, Int] => dtItM.minBy(_._2)._2
And the same for Double and BigDecimal cases.
How is that the compiler can not even recognize Int to apply default ordering? Curious thing is that it desn’t seem to have any problem with a BigDecimal field of my own custom class.
Well,
Int <: T with Int. Now, if you are going to useOrdering[Int]for it, then you needOrdering[T with Int] <: Ordering[Int], which meansOrderingmust be contravariant. Unfortunately,Orderingis not contravariant (even though it could — and it is in Scalaz).