# New Issue: Range +, - operator semantics

17101, “vasslitvinov”, “Range +, - operator semantics”, “2021-02-05T21:06:56Z”

Given a range `r` and an integer `i`, what should the following mean?

• `r + i`
• `r - i`
• `i + r`
• `i - r`

These look like promotion. However, in 70c4746484 we introduced special cases for the first three so that they return an appropriately-modified range. It was in response to @cassella’s suggestion - or at least motivated by it - as documented in d1df3ba497. Aside: back then we had open-interval domains (or ranges?) in Chapel, so instead of writing `0..#n` we wrote `[0..n)`.

The motivation, rephrased in today’s terms, was that given a math expression with a range, we wanted to represent its result also as a range, rather than as an iterable (which would be the outcome of promotion). Perhaps this is so that an array could be sliced with such a math expression just like it can be sliced with a range:

``````var A = [1, 2, 3, 4, 5];
var r = 1..2;
writeln(A[r*2-1]);
``````

Cf. for some time we had similar special cases for `*` and `/`. Those were removed in d515ff7437 with this explanation: “With the introduction of the # operator, they are not nearly as necessary, and they always resulted in certain ambiguities/issues that nobody was terribly comfortable with: e.g., what does `r * 0` mean? What does `r * -1` mean? What does `(1..n by 1) / 2` mean?”

That same commit further stated: “I still believe `+` and `-` are important on ranges due to their intuitiveness, cleanness, and the fact that they keep the operations closed on ranges without any confusing cases.”

Today, if we removed the special cases for `r+i` etc., the above array slicing expression would mean a promoted indexing operation.

We rely on this special meaning of `+` and `-`, for example, in our rectangular domain maps to “translate” and un-translate domains/ranges when passing the current iteration subspace from the leader to the followers. There, we could instead invoke named methods to achieve the same outcome.

#### Conclusion?

In today’s range module review, we leaned towards removing this special handling of + and -, therefore reverting them to mean a promoted op.

Pro: eliminate confusion about what `r + i` means and that `r-i` and `i-r` work differently.

Con: will require more verbose syntax when the range result is desired, ex. have to spell out `r.translate(i)`.