据我了解,以下代码段应该可以正常执行。 Stream("A", "").toIterable.map(_.head)
抛出异常,应由try
块捕获。
import org.scalatest.FunSuite
class TryCatchTest extends FunSuite {
test("Try failure") {
List(1).flatMap {
_ =>
try {
Stream("A", "").toIterable.map(_.head)
} catch {
case e: Throwable => Nil
}
}
}
}
但是当我运行它时,它崩溃并出现以下错误:
next on empty iterator
java.util.NoSuchElementException: next on empty iterator
at scala.collection.Iterator$$anon$2.next(Iterator.scala:39)
at scala.collection.Iterator$$anon$2.next(Iterator.scala:37)
at scala.collection.IndexedSeqLike$Elements.next(IndexedSeqLike.scala:63)
at scala.collection.IterableLike$class.head(IterableLike.scala:107)
at scala.collection.immutable.StringOps.scala$collection$IndexedSeqOptimized$$super$head(StringOps.scala:30)
at scala.collection.IndexedSeqOptimized$class.head(IndexedSeqOptimized.scala:126)
at scala.collection.immutable.StringOps.head(StringOps.scala:30)
at TryCatchTest$$anonfun$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$1.apply(TryCatchTest.scala:8)
at TryCatchTest$$anonfun$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$1.apply(TryCatchTest.scala:8)
at scala.collection.immutable.Stream$$anonfun$map$1.apply(Stream.scala:418)
at scala.collection.immutable.Stream$$anonfun$map$1.apply(Stream.scala:418)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1222)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1212)
at scala.collection.immutable.Stream.foreach(Stream.scala:595)
at scala.collection.immutable.List.flatMap(List.scala:327)
at TryCatchTest$$anonfun$1.apply$mcV$sp(TryCatchTest.scala:5)
at TryCatchTest$$anonfun$1.apply(TryCatchTest.scala:5)
at TryCatchTest$$anonfun$1.apply(TryCatchTest.scala:5)
at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:166)
at org.scalatest.Suite$class.withFixture(Suite.scala:1122)
at org.scalatest.FunSuite.withFixture(FunSuite.scala:1555)
at org.scalatest.FunSuiteLike$class.invokeWithFixture$1(FunSuiteLike.scala:163)
at org.scalatest.FunSuiteLike$$anonfun$runTest$1.apply(FunSuiteLike.scala:175)
at org.scalatest.FunSuiteLike$$anonfun$runTest$1.apply(FunSuiteLike.scala:175)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
at org.scalatest.FunSuiteLike$class.runTest(FunSuiteLike.scala:175)
at org.scalatest.FunSuite.runTest(FunSuite.scala:1555)
at org.scalatest.FunSuiteLike$$anonfun$runTests$1.apply(FunSuiteLike.scala:208)
at org.scalatest.FunSuiteLike$$anonfun$runTests$1.apply(FunSuiteLike.scala:208)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:413)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:401)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401)
at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:396)
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:483)
at org.scalatest.FunSuiteLike$class.runTests(FunSuiteLike.scala:208)
at org.scalatest.FunSuite.runTests(FunSuite.scala:1555)
at org.scalatest.Suite$class.run(Suite.scala:1424)
at org.scalatest.FunSuite.org$scalatest$FunSuiteLike$$super$run(FunSuite.scala:1555)
at org.scalatest.FunSuiteLike$$anonfun$run$1.apply(FunSuiteLike.scala:212)
at org.scalatest.FunSuiteLike$$anonfun$run$1.apply(FunSuiteLike.scala:212)
at org.scalatest.SuperEngine.runImpl(Engine.scala:545)
at org.scalatest.FunSuiteLike$class.run(FunSuiteLike.scala:212)
at org.scalatest.FunSuite.run(FunSuite.scala:1555)
at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:55)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$3.apply(Runner.scala:2563)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$3.apply(Runner.scala:2557)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:2557)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1044)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1043)
at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:2722)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1043)
at org.scalatest.tools.Runner$.run(Runner.scala:883)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
有趣的是,如果我做出以下任何更改,它将按预期运行:
flatMap
替换为map
"A"
替换为""
答案 0 :(得分:8)
问题是来自Stream
/ Iterable
的延迟评估。如果你将它分解成更小的部分,这很容易看出来。
scala> Stream("A", "").toIterable.map(_.head)
res7: Iterable[Char] = Stream(A, ?)
将Stream
映射到_.head
意味着我们将字符串视为集合,而空字符串作为集合没有head
,因此它会抛出异常。但是它不会立即抛出异常,因为Iterable
仍然像Stream
一样懒惰,如上所示。只评估第一个元素。如果我们将它设置为非惰性,则抛出异常:
scala> Stream("A", "").toIterable.map(_.head)
java.util.NoSuchElementException: next on empty iterator
或者,如果您将"A"
替换为""
,您也会收到异常,因为Stream
的头部会被评估并映射到head
,没有。
scala> Stream("", "").toIterable.map(_.head).toList
java.util.NoSuchElementException: next on empty iterator
代码中唯一会出现的例外情况是,head
或Stream
的{{1}}会将其抛出。好的,那么Iterable
为什么不抛出异常,但是map
呢?好吧,这个flatMap
实际上并没有对map
执行任何操作:
Stream
因此,未被评估的成员保持不变。但scala> List(1).map { _ => Stream("A", "").toIterable.map(_.head) }
res11: List[Iterable[Char]] = List(Stream(A, ?))
会尝试将flatMap
展平为一个集合,因此必须对它们进行评估以制作新集合。这样做会抛出异常:
Iterable