为什么在 GenericTraversableTemplate 上声明 flatten 而不是在 TraversableLike 上声明?

发布于 2024-08-12 17:37:09 字数 647 浏览 4 评论 0原文

TraversableLike.flatMap 的签名如下:

def flatMap[B, Th](f : (A) => Traversable[B])(implicit bf : CanBuildFrom[Repr, B, Th]) : Th

GenericTraversableTemplate.flatten 的签名是:

def flatten[B](implicit asTraversable : (A) => Traversable[B]) : CC[B] 

为什么是后一种方法(在我看来与 flatMap< /code> 仅在变压器函数是隐式的意义上)不能在 TraversableLike 上定义为:

def flatten[B, Th](implicit asTraversable: (A) => Traversable[B], 
                   implicit bf : CanBuildFrom[Repr, B, Th]) : Th

是否有某种原因表明情况必须如此?

The signature of TraversableLike.flatMap is as follows:

def flatMap[B, Th](f : (A) => Traversable[B])(implicit bf : CanBuildFrom[Repr, B, Th]) : Th

The signature of GenericTraversableTemplate.flatten is:

def flatten[B](implicit asTraversable : (A) => Traversable[B]) : CC[B] 

Why is the latter method (which seems to me to differ from flatMap only in the sense that the transformer function is implicit) not definable on TraversableLike as:

def flatten[B, Th](implicit asTraversable: (A) => Traversable[B], 
                   implicit bf : CanBuildFrom[Repr, B, Th]) : Th

Is there some reason that this must be the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椵侞 2024-08-19 17:37:09

我认为答案就在源代码中:

def flatten[B](implicit asTraversable: A => /*<:<!!!*/ Traversable[B]): CC[B]

flatten 的隐式参数实际上应该是 A <:< Traversable[B] (即,GenericTraversableTemplate 的泛型参数本身就是 Traversable 的断言)。 (请参阅此邮件列表线程 讨论为什么它目前被注释掉。)我的理解是,这个特征中的所有方法过去都是在集合类的(一些)伴随对象上定义的,因为它们只适用于某些实例化(如果是这样的话)类型参数的正确单词)。这个 <:< 构造允许它们成为实例方法。

I think the answer lies in the source code:

def flatten[B](implicit asTraversable: A => /*<:<!!!*/ Traversable[B]): CC[B]

The implicit parameter to flatten should actually be A <:< Traversable[B] (ie. an assertion that the generic parameter of GenericTraversableTemplate is itself Traversable). (See this mailing list thread for discussion about why it's currently commented out.) My understanding is that all the methods in this trait used to be defined on (some of) the companion objects of collection classes because they were only applicable for some instantiations (if that's the right word) of type parameters. This <:< construct allows them to be made instance methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文