Hacker News new | past | comments | ask | show | jobs | submit login

I feel like it's nim that made the bad choice here. Not because one meaning of .. naturally makes more sense than another but because it breaks the expectations set by other languages.



But Nim isn't unique. Ada is, now, approaching 40 years old since publication. It uses closed intervals:

  1..12 = [1,12]
With no syntactic notion for half-open intervals.


I didn't realize that the syntax dated back to Ada. Though interestingly it doesn't feel off to me in the context of a 1-based index language.


Ada is actually not 1-based. Arrays are accessed using an index type which is defined at declaration. It can be any discrete type, including ranges starting at 0 (or 1, or -1, or anything else which fits the domain).


Interesting! I looked at a few different sites for examples of arrays in Ada and saw them all starting at 1. But that's a neat feature that it lets you pick the start and end.


Ada isn't even 1-based, though many of its built-in array-based types are 1-based. It's "whatever ascending, consecutive range you want"-based. Which, IMHO, is the best option. You can use 0-based where it makes sense and 1-based where it makes sense, and -100 based where it makes sense, or 'a'..'z' based where it makes sense.

And I think that last example demonstrates why the closed range notation makes a lot of sense over half-open (at least for languages that permit describing ranges of arbitrary discrete types). When talking about numbers a half-open range notation is clear and useful because numbers exist in an obvious sequence (so long as we're sticking to some subset of the real numbers, complex numbers don't have as obvious an ordering). But when talking about other ranges, like characters, how would you use a half-open range (in a clear way) to describe the characters a-z? 'a'..'{'? Is that actually clear?

What about describing the range when you want to include the last valid value? How do I describe, using the byte type itself, a range 0 through 255:

  type Byte is mod 256;
  subtype Full_Range is Byte range 0..255; -- valid Ada, closed range notation
  subtype Full_Range is Byte range 0..256; -- invalid Ada, half-open notation
I'm now describing my range of a specific type using an element that isn't part of the type. For numbers this can work. But what is the character after the last character of the set of characters?

  subtype Characters_After_And_Including_Lower_A is Character range 'a'..???;
  -- presumably we'd have a better name in a real application
In a half-open notation that's impossible to write and be explicit about the upper bound. You can only describe it with an implicit upper bound by leaving the ??? blank as in:

  subtype Characters_After_And_Including_Lower_A is Character range 'a'..;
A valid solution. Though in Ada this would be a no-go as it leaves open some ambiguity/possibility for error in the use of the `..` notation (what if the author didn't mean for it to run to the end but forgot to type the upper bound?), so a second notation would have to be formed. Perhaps a third `.` or some symbol indicating that it's open-ended:

  Character range 'a' ...; -- perhaps?


Yes it is convenient to express subsets/subtypes as closed intervals. But it doesn't cover all CS nicely.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: