Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[Feature] Handle reverse range

Open mks-h opened this issue 5 months ago • 9 comments

The range expression (1..12/1..=12) can be written in reverse, like 12..1/12..=1. This will compile just fine, but on runtime a reversed range like this will not expand to anything (echo 12..1 outputs an empty string). For the reference, the range may include a variable, so we cannot always know whether the range is reversed or not.

First of all, we need to decide whether to allow reversed range expressions (12..1 => seq 12 -1 1), or not. And if not, we can think about providing an alternative syntax for them.

Whether we allow it or not, we can detect that a range is reversed in two ways:

  1. At compile time, when using numeric literals (12..1)
  2. If there's a variable, with a runtime conditional before doing seq

If we decide to forbid reverse range, we should make the runtime-checked range failable and error out when it is indeed reversed. If it's known to be reversed at compile time, the compiler should error out.

If we decide not to forbid the reverse range, there's no need to do anything with the literal reversed ranges like 12..1, as it is obvious that the range is reversed. But if there's a variable, the programmer might assume it is going to be a forward range, despite it possibly being reversed. So we should probably find a way to handle both cases in code. On the other hand, this might easily make it annoying to write ranges.

mks-h avatar Sep 08 '24 21:09 mks-h