Why can’t I do this:
def compare[A <% { def toInt: Int },
B <% { def toInt: Int }]
(bs: Seq[A], is: Seq[B]): Boolean = {
bs.toArray.zip(is) forall { p => p._1.toInt == p._2.toInt }
}
In order to compare any Sequence of types convertible to Int? How can I implement a similar pattern?
Update: this should run Message.compare(List(1.0, 2.0, 3.0), List(0, 0, 0))
A good example of where the problem lies would seem to be doing this:
Scala 2.9.1 blows up at this point sometime in Runtime code – I can only think that this is a bug.
You can achieve the effect you’re looking for using type classes, though: the following code works in all of your examples:
This should also have the benefit of being faster than the version using structural types. If you need to add your own types in that should be convertible to integers, you can provide evidence for the Numeric typeclass in the same way as is done at https://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_9_1_final/src//library/scala/math/Numeric.scala#L1 for the standard values.