【发布时间】:2017-06-21 04:03:31
【问题描述】:
我有下面的简单代码
private def tt(l:Seq[Int]):Seq[Future[Unit]] = {
val aa = l.map(_=>Future.successful(()))
aa
}
我可以很容易地掌握我返回Future[Unit] type corresponding to each element of my sequence "l" that is passed to method tt. 的事实。因此很容易说"aa" 是Seq[Future[Unit]] 类型。我无法理解的是,当我的“l”序列的长度为零时,map 将永远不会被执行,为什么以及如何才能获得 Seq[Future[Unit]] 因为我有明确输入返回类型,显然 scala 很高兴无论“l”是什么,即使为空,“aa' 仍然会导致 Seq[Future[Unit]]。但我不明白为什么
【问题讨论】:
-
一个空的 Seq 真的可以形成任何类型。它只是空的。不管它是零
Ints 还是零Future[Unit]s。它仍然什么都没有。None和Option也一样,空的 Option 可以是任何类型,因为它没有实际值。