Scala:可遍历的foreach定义 [英] Scala: Traversable foreach definition

查看:52
本文介绍了Scala:可遍历的foreach定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想 Traversable 特性中的 foreach 方法定义如下:

I suppose foreach method in Traversable trait is defined as follows:

def foreach[U](f: Elem => U)

应将其定义为 foreach [U,Elem](f:Elem => U),因为有两种类型, Elem U 吗?

Should it be defined as foreach[U,Elem](f:Elem =>U) as there are two types, Elem and U ?

推荐答案

foreach [U](A => U)是特征 Traversable [+ A] .

trait Traversable[+A] extends TraversableLike[A, ...] {
  ...
}

trait TraversableLike[+A, ...] extends ... {
  ...

  def foreach[U](f: A => U): Unit

  ...
}

https://github.com/scala/scala/blob/2.12.x/​​src/library/scala/collection/TraversableLike.scala#L124

这篇关于Scala:可遍历的foreach定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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