views:

104

answers:

1

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)

+7  A: 

Is(and why) this really should be prohibited with exception?

Quoting from scaladoc:

The Range class represents integer values in range [start;end) with non-zero step value step. 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. 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.

Dario
thnx, should really be more attentive in reading scaladoc
sgp