I used to think both of Odin’s representations were pretty good. But after thinking about it a bit more, now I hope that no matter which one is used, there’s at least only one range syntax.
I agree that both the left-closed right-open and left-closed right-closed approaches have their advantages, but they also have their own pitfalls. In my view, their pros and cons almost always exist, so choosing one basically means accepting its problems for the sake of its benefits. That’s why I think mixing them is always worse, because then you constantly have to be aware of which one you’re using and watch out for the potential footgun of each approach.
The advantages of left-closed right-open intervals are clearly explained by Dijkstra, but their drawbacks are also obvious. If you want to represent a range of n-digit numbers, you have to introduce n+1-digit numbers. Otherwise, you might run into overflow footguns.
Zig’s approach is to require that all ranges must be usize. Obviously, this isn’t the most pleasant approach, as it forces you to use a lot of annoying @intCast and you can’t use negative numbers. But after paying that price, we get left-closed, right-open range that feel intuitive to programmers, along with all the advantages that Dijkstra praised. Since usize is huge enough that you basically can’t traverse it completely, this hacky method effectively avoids overflow risks.
Using closed intervals on both ends avoids overflow issues, but it sacrifices all the benefits that Dijkstra praised and goes against programmer intuition. Almost all modern programming languages use left-closed, right-open intervals, which has become the norm, and going against it will definitely cause more bugs.
But anyway, after thinking it over, I still believe there should only be one way to represent a range, no matter what flaws it has.
Maybe there’s a win-win solution: using the left-closed, right-open approach, while also being able to automatically handle overflow issues. Hopefully #3806 can make this happen.