问题
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
?
回答1:
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 was10
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 fromend
.
来源:https://stackoverflow.com/questions/12439157/exclusive-end-in-scalas-range-class