在 REPL 中除以零时省略了哪些 28 帧?
What 28 frames are elided when dividing by zero in the REPL?
scala> 5 / 0
java.lang.ArithmeticException: / by zero
... 28 elided
一个简单的算术表达式省略了28帧?!这些框架是什么,为什么 Scala 需要 那么多 来进行安全除法,为什么首先要删除它们?
scala> import scala.util.Try
import scala.util.Try
scala> Try(5/0)
res2: scala.util.Try[Int] = Failure(java.lang.ArithmeticException: / by zero)
scala> res2.recover { case e: ArithmeticException => e.printStackTrace }
java.lang.ArithmeticException: / by zero
at $line8.$read$$iw$$iw$$anonfun.apply$mcI$sp(<console>:13)
at $line8.$read$$iw$$iw$$anonfun.apply(<console>:13)
at $line8.$read$$iw$$iw$$anonfun.apply(<console>:13)
at scala.util.Try$.apply(Try.scala:192)
at $line8.$read$$iw$$iw$.<init>(<console>:13)
at $line8.$read$$iw$$iw$.<clinit>(<console>)
at $line8.$eval$.$print$lzycompute(<console>:7)
at $line8.$eval$.$print(<console>:6)
at $line8.$eval.$print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:786)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:1047)
at scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq.apply(IMain.scala:638)
at scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq.apply(IMain.scala:637)
at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
at scala.reflect.internal.util.AbstractFileClassLoader.asContext(AbstractFileClassLoader.scala:19)
at scala.tools.nsc.interpreter.IMain$WrappedRequest.loadAndRunReq(IMain.scala:637)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:569)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:565)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:807)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:681)
at scala.tools.nsc.interpreter.ILoop.processLine(ILoop.scala:395)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:415)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process.apply$mcZ$sp(ILoop.scala:923)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process.apply(ILoop.scala:909)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process.apply(ILoop.scala:909)
at scala.reflect.internal.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:97)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:909)
at scala.tools.nsc.MainGenericRunner.runTarget(MainGenericRunner.scala:74)
at scala.tools.nsc.MainGenericRunner.run(MainGenericRunner.scala:87)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
res3: scala.util.Try[AnyVal] = Success(())
省略的行基本上是 REPL 读取一行的开销,将其编译成 class,并构造 class 的实例,这就是 REPL 中编写的代码所在评价。
scala> 5 / 0
java.lang.ArithmeticException: / by zero
... 28 elided
一个简单的算术表达式省略了28帧?!这些框架是什么,为什么 Scala 需要 那么多 来进行安全除法,为什么首先要删除它们?
scala> import scala.util.Try
import scala.util.Try
scala> Try(5/0)
res2: scala.util.Try[Int] = Failure(java.lang.ArithmeticException: / by zero)
scala> res2.recover { case e: ArithmeticException => e.printStackTrace }
java.lang.ArithmeticException: / by zero
at $line8.$read$$iw$$iw$$anonfun.apply$mcI$sp(<console>:13)
at $line8.$read$$iw$$iw$$anonfun.apply(<console>:13)
at $line8.$read$$iw$$iw$$anonfun.apply(<console>:13)
at scala.util.Try$.apply(Try.scala:192)
at $line8.$read$$iw$$iw$.<init>(<console>:13)
at $line8.$read$$iw$$iw$.<clinit>(<console>)
at $line8.$eval$.$print$lzycompute(<console>:7)
at $line8.$eval$.$print(<console>:6)
at $line8.$eval.$print(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:786)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:1047)
at scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq.apply(IMain.scala:638)
at scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq.apply(IMain.scala:637)
at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
at scala.reflect.internal.util.AbstractFileClassLoader.asContext(AbstractFileClassLoader.scala:19)
at scala.tools.nsc.interpreter.IMain$WrappedRequest.loadAndRunReq(IMain.scala:637)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:569)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:565)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:807)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:681)
at scala.tools.nsc.interpreter.ILoop.processLine(ILoop.scala:395)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:415)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process.apply$mcZ$sp(ILoop.scala:923)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process.apply(ILoop.scala:909)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process.apply(ILoop.scala:909)
at scala.reflect.internal.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:97)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:909)
at scala.tools.nsc.MainGenericRunner.runTarget(MainGenericRunner.scala:74)
at scala.tools.nsc.MainGenericRunner.run(MainGenericRunner.scala:87)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
res3: scala.util.Try[AnyVal] = Success(())
省略的行基本上是 REPL 读取一行的开销,将其编译成 class,并构造 class 的实例,这就是 REPL 中编写的代码所在评价。