简体   繁体   English

scala new步长等于零的范围

[英]scala new Range with step equals zero

Is(and why) this really should be prohibited with exception? 是(和为什么)这应该被禁止,但例外?

scala> val r2 = 15 until (10, 0)

java.lang.IllegalArgumentException: requirement failed

scala> new Range(10,15,0)

java.lang.IllegalArgumentException: requirement failed
    at scala.Predef$.require(Predef.scala:133)

Is(and why) this really should be prohibited with exception? 是(和为什么)这应该被禁止,但例外?

Quoting from scaladoc : 引自scaladoc

The Range class represents integer values in range [start;end) with non-zero step value step. Range类表示范围为[start; end]的整数值, 为非零步长值。 Sort of acts like a sequence also (supports length and contains). 类似于序列的行为(支持长度和包含)。

This restriction makes sense. 这种限制是有道理的。 A range with step-size zero would always be infine and just consist of the lower bound value. 步长为零的范围始终为infine,只包含下限值。 Whereas one could argue that infinite ranges are possible (lazy evaluation), the concept of an upper bound in the range would be taken ad absurdum. 虽然有人可能认为无限范围是可能的(懒惰评估),但范围上限的概念将被视为荒谬。 A range with step 0 is simply not a range , even if it's infinitely long, because the upper bound has no importance. 步骤0的范围不是范围 ,即使它是无限长的,因为上限没有重要性。

So if one really wants an infinite stream of a single value, Scala rightfully forces us to be more explicit. 因此,如果一个人真的想要一个单一值的无限流,Scala正确地迫使我们更加明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM