I have a test which fails intermittently because of ordering issues when I iterate over the values in a Map.
Scala helpfully provides a ListMap which makes the tests stable, at the expense of performance. So I abstracted the ImmutableMapFactory as a val and use it in my code.
class C {
val immutableMapFactory = scala.collection.immutable.Map
def func = {
...
immutableMapFactory(pairs :_*)
}
}
Now my plan was to extend C and override immutableMapFactory for tests
class TestableC extends C {
override val immutableMapFactory = scala.collection.immutable.ListMap
}
Unsurprising this fails as ListMap does not have the same type as Map. How should I specify the type of the val (or a def) so that I can use the factory wherever I need to create a Map?
Your problem is in this line:
This makes
immutableMapFactoryequal to the singleton objectMap.ListMap(the singleton) is not a subclass ofMap(the singleton), so the subsequent override fails.If you instead take the
applymethod fromMap, and partially apply it to form a first class function (of type(A, B)* => immutable.Map[A,B]) then the technique can be made to work: