Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type parameter of minBy[B](f: ((A, B)) ⇒ B)(implicit cmp: Ordering[B]): (A, B)

The trait Map[A,+B] has a method

def minBy [B] (f: ((A, B)) ⇒ B)(implicit cmp: Ordering[B]): (A, B)

I expected the B of the trait to be the same as the one in the method, but then I can't still make sense of this:

val m2 = Map('a -> "1", 'b ->"2" ,'c ->"3")
m2.minBy((t:(Symbol,String))=>Integer.parseInt(t._2))

Here, B of Map[A,+B] is String, but B of minBy is Int - or err I?

So I expected the type of the method to be rather

def minBy [C] (f: ((A, B)) ⇒ C)(implicit cmp: Ordering[C]): (A, B)

But thats not what the source says.

If both are distinct, where should I have known? If they are not - can you spot, and point out, my mistake?

like image 656
Theodosius Avatar asked Dec 06 '11 09:12

Theodosius


2 Answers

It seems like the software that builds the documentation has simply failed to rename the B variable from the trait's definition of minBy, thus causing a name clash. Your analysis seems correct.

To use the terminology from lambda calculus, I'd say that the software failed to alpha-convert.

like image 61
dhg Avatar answered Nov 11 '22 07:11

dhg


You analysis is correct, it should be renamed as C, or something else. The problem is that scaladoc is simply replacing the A in the definition in TraversableLike with a Tuple (A, B) because it's a map. This is the definition from TraversableLike:

def minBy [B] (f: (A) ⇒ B)(implicit cmp: Ordering[B]): A

because it's a map, scaladoc replaces the (A) with a tuple (A, B).

def minBy [B] (f: (A, B) ⇒ B)(implicit cmp: Ordering[B]): (A, B)

which as you observe, isn't actually the correct signature.

This is a known issue, scaladoc does not disambiguate between same-named type parameters. Vote it up or submit a patch!

like image 27
Matthew Farwell Avatar answered Nov 11 '22 08:11

Matthew Farwell