Exclusive Scala End Range

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

 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 

Not worth res0 == 11 ?

+6
source share
1 answer

to and until lead to related but different classes of Range : Range.Inclusive and Range respectively; Range.Inclusive IS-A Range .

  • isInclusive will distinguish between two types of classes.

  • end , or the upper bound, is interpreted in the context of a range that is exclusive or included. The upper bound is the second number indicated in the range specification, which was 10 in both examples in the original question. And yes, this “upper limit” may be less than the “lower limit” if the range goes from high to low (ie 1 until -10 ).

  • last will return the last value in the range, which may be what you tried to get from end .

+12
source

Source: https://habr.com/ru/post/925475/


All Articles