scala new Range,步长为零
这真的应该被禁止吗(以及为什么)?
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?
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引用 scaladoc:
这个限制是有道理的。步长为零的范围始终是无限的,并且仅包含下界值。尽管有人可能认为无限范围是可能的(惰性求值),但范围上限的概念将被视为荒谬的。步长为 0 的范围根本就不是范围,即使它无限长,因为上限并不重要。
因此,如果人们真的想要单个值的无限流,Scala 理所应当地迫使我们更加明确。
Quoting from scaladoc:
This restriction makes sense. A range with step-size zero would always be infine and just consist of the lower bound value. 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.
So if one really wants an infinite stream of a single value, Scala rightfully forces us to be more explicit.