« Return to Thread: [scala] Using existential types to get rid of extraneous type parameters

[scala] Using existential types to get rid of extraneous type parameters

by Paul Chiusano-2 :: Rate this Message:

Reply to Author | View in Thread

Hi, I am experimenting with using existential types to get rid of some extraneous type parameters, however, I'm not totally sure what I'm doing and was hoping someone would enlighten me.

Take the following function:

  def integerDivide[K](denominator: Int, vs: Map[K,Int]): Map[K,Int] =
    vs.transform((k,v) => v/denominator)

It seems silly to have K appear as a type parameter to this function since it is not "touched" by the body of the function. So I tried this:

  def integerDivide(denominator: Int)(vs: Map[_,Int]): Map[_,Int] =
    vs.transform((k,v) => v/denominator)

That doesn't work either, though - the following doesn't type check:

   val divided: Map[String,Int] = integerDivide(3)(Map(("a", 1), ("b", 2)))

I get an error like:

type mismatch;
 found   : Map[_$5,Int] where type _$5
 required: Map[String,Int]

Is there any way I can get the type to carry through without having to make the key a type parameter? Or is what I am trying to do just crazy, not-well defined, or not possible?

Paul

 « Return to Thread: [scala] Using existential types to get rid of extraneous type parameters

LightInTheBox - Buy quality products at wholesale price!