Say I have a higher kinded type
SuperMap[Key[_],Value[_]]`.
Suppose now that I had something even more specific that required that the type parameter for Key must match that for Value; that is, something like:
SuperDuperMap[T, Key[T], Value[T]]
Further suppose that I didn’t want just any T, but a very specific one where T <: OtherT
SuperDuperPooperMap[T <: OtherT, Key[T], Value[T]]
Can this be done in Scala? Is this just generally a bad idea? Is there an equivalent way of doing this that’s easier to read/write/use?
Your declaration already works as supposed to, i.e. you’re restricting the type of
Tas well asKeyandValue. The way you’ve written it, however, scala will complain if you issue something likebecause the types of both
KeyandValueare already given by your former declaration. Hence this will workwhich is probably not want you want. You could do it like this
At the bottom line, since the types of
KeyandValuedepend solely onTit is somewhat superfluous to have all that redundant information when working withFoo. So why not use an inner type declaration like so:Then you’d have access to types
KandVfrom within the class but wouldn’t need to type it everytime you create a new answer: