Scala子字符串正确抛出IndexOutOfBounds,但不会在Scala.js中抛出

时间:2019-01-22 17:51:16

标签: scala scala.js

在Scala REPL中,当在太短的字符串上调用子字符串以至于无法剪切时,它会按预期将索引超出范围:

scala> "123".substring(0,6)
java.lang.StringIndexOutOfBoundsException: String index out of range: 6
  at java.lang.String.substring(String.java:1963)
  ... 32 elided

但是在Scala.js中运行上述操作不会引发异常:

"123".substring(0,6)

// returns "123"

为什么会这样?

1 个答案:

答案 0 :(得分:3)

以下是an almost identical question的维护者答案:

  

也就是说,不,我们不会保持一致,因为StringIndexOutOfBounds是Scala.js中未定义的行为。

您可以检查the documentation of the semantics of Scala.js来确认此主张,但是与substring有关的这种特殊情况以前使我感到困惑,因此应该在该页面上更具体地注明。