Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclusive end in Scala's Range class

Tags:

range

scala

According to the Scala documentation for the method Range.end, it returns "the exclusive end of the range." So why does it return the same value for both the to and the until notations? For example:

Welcome to Scala version 2.9.2 (Java HotSpot(TM) Server VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.

scala> (1 to 10).end
res0: Int = 10

scala> (1 until 10).end
res1: Int = 10

Shouldn't res0 == 11?

like image 844
Otavio Macedo Avatar asked Sep 15 '12 16:09

Otavio Macedo


1 Answers

to and until result in related but different Range classes: Range.Inclusive and Range, respectively; Range.Inclusive IS-A Range.

  • isInclusive will distinguish between the two class types.

  • end, or the upper bound, is interpreted in the context of the range being exclusive or inclusive. The upper bound is the second number provided in the range specification, which was 10 in both of the sample cases in the original question. And yes, this "upper bound" may be less than the "lower bound", if the range goes from high to low (i.e. 1 until -10).

  • last will return the last value in the range, which may be what you were trying to obtain from end.

like image 51
Richard Sitze Avatar answered Sep 22 '22 11:09

Richard Sitze