Can this be done?
case class Foo[T](value: T)
case class Bar[T](value: T)
val map = mutable.Map.empty[Foo[_], Bar[_]]
map(Foo(1)) = Bar(1) // correct
map(Foo(1)) = Bar(1.1) // should cause a compiler error
It should allow any type T
, as long as both Foo#T
and Bar#T
are the same.
Map
alone won't allow you to enforce that constraint, becauseMap
takes two type parameters that are completely independent. You could define a wrapper class (around aMap
) that would take a single type parameter (for both the keys and values of the underlyingMap
). – jub0bs