3 ms·
Did I read that right about the range operators: triple-dot (...) is like the inclusive range that perl, python, ruby (etc?) denote with double-dot (..), and i
by Roboprog 12y ago
Did I read that right about the range operators: triple-dot (...) is like the inclusive range that perl, python, ruby (etc?) denote with double-dot (..), and in swift double-dot (..) is actually a special range operator that excludes the upper limit?
If so, that seems backwards, and too late to fix :-(
(not huge, but unfortunate)
- hrabago 12y agoI can't talk about the comparison to perl/python/ruby, but yes, using the triple dot includes the upper limit, and using the double-dot excludes it. For the typical (for var i=0; i < count-1; i++) scenario, you'd using the double dot (i in 0..count).
- roryokane 12y agoI always thought that Ruby had it backwards, so I’m glad it’s different in Swift. I think it makes more sense for the triple-dot ... range to mean an inclusive range, because you type an extra dot at the end compared to the double-dot .. range, and that extra dot means that the range includes an extra number on the end.
- jacobolus 12y agoPython has much more sensible range operations (based on slice syntax for array slices, or the arguments to the build-in 'range' function) that are unrelated to this. Ruby (and coffeescript, and perhaps perl) uses a..b to indicate a closed range (i.e. including b), or a...b to indicate a half-closed range (not including b). Swift seems to do the opposite. My guess for how ruby (or predecessors) came up with this was by copying bash (or sh?), which has only the .. version. To be honest, the .. vs. ... distinction is really confusing and error-prone, and a bad bit of syntax to include in any language, period.
- andybak 12y agoFirst thing I looked for in Swift was support for Python's slicing syntax. It's a shame they decided against using that.
- masklinn 12y ago* Python has no dots for ranges, it has a range() builtin function and it has slices which uses colon (e.g. some_array[1:42:2]). Both range and slices exclude the upper bound * the Swift operators makes a surprising amount of sense (where Ruby's make very little): the longer operator yields the longer range and `0..10` yields a range of length 10. Think of it like this: the "center": dot is the range itself, the left dot includes the left bound, and the right dot includes the right bount. Thus `..` is "range with left bound included` and `...` is "range with right bound included"