Exclusive end in Scala's Range class

北城余情 提交于 2019-12-10 13:38:46

问题


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 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.



来源:https://stackoverflow.com/questions/12439157/exclusive-end-in-scalas-range-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!