minBy[B](f: ((A, B)) ⇒ B)(implicit cmp: Ordering[B]) 的类型参数:(A, B) [英] Type parameter of minBy[B](f: ((A, B)) ⇒ B)(implicit cmp: Ordering[B]): (A, B)

查看:48
本文介绍了minBy[B](f: ((A, B)) ⇒ B)(implicit cmp: Ordering[B]) 的类型参数:(A, B)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

特征 Map[A,+B] 有一个方法

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

我希望特征的 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))

这里,Map[A,+B]BString,而B>minByInt - 还是 err I?

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?

推荐答案

你的分析是对的,应该改名为C,或者别的什么.问题在于,scaladoc 只是用元组 (A, B) 替换了 TraversableLike 中定义中的 A,因为它是一个映射.这是 TraversableLike 的定义:

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

因为它是一个映射,scaladoc 用一个元组 (A, 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.

这是一个已知问题,scaladoc 不会消除同名类型参数之间的歧义.投票或提交补丁!

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

这篇关于minBy[B](f: ((A, B)) ⇒ B)(implicit cmp: Ordering[B]) 的类型参数:(A, B)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆