Thread overview
[Issue 24649] Upper-bound-inclusive range foreach
Jul 05
Bolpat
Jul 05
Dennis
4 days ago
Bolpat
July 05
https://issues.dlang.org/show_bug.cgi?id=24649

Bolpat <qs.il.paperinik@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |qs.il.paperinik@gmail.com

--
July 05
https://issues.dlang.org/show_bug.cgi?id=24649

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl

--- Comment #1 from Dennis <dkorpel@live.nl> ---
Adding magic rules to the + operator in a specific context is a bad idea. More feasable would be to extend the Range element as a whole, for example like in odin:

```
for i in 0..=9 {}
for i in 0..<10 {}
```

https://odin-lang.org/docs/overview/#range-based-for-loop

--
4 days ago
https://issues.dlang.org/show_bug.cgi?id=24649

--- Comment #2 from Bolpat <qs.il.paperinik@gmail.com> ---
(In reply to Dennis from comment #1)
> More feasable would be to extend the Range element as a whole, for example like in odin:
> 
> ```
> for i in 0..=9 {}
> for i in 0..<10 {}
> ```
> 
> https://odin-lang.org/docs/overview/#range-based-for-loop

I get where this is coming from, but in D, already, `..` is with exclusive upper bounds. What I’ve seen, `..` or `...` means inclusive upper bound and `..<` is used for exclusive. We can’t do that.

I really thought about that, and just nothing came to my mind that really makes
sense. We could use `foreach (i; L ... U)` for inclusive upper bounds, but a
single `.` making the difference maybe isn’t a great idea, and, more
importantly, `..` looks like the inclusive and `...` the exclusive one, when
it’s exactly reverse. (I’d bet there’s a language that does exactly this, but I
don’t remember which.)

What I’d take from Odin is `..<=`, but not `..=`. A downside I can see people being confused because there being `..<=` implies there is `..<` but that one wouldn’t exist. (There would have to be added a special error message telling people to use just `..` instead of `..<`.)

> Adding magic rules to the + operator in a specific context is a bad idea.

I don’t see why exactly this is the case. Technically speaking, it wouldn’t even be the `+` operator, it’s part of the `foreach` syntax, similar how in an argument list, or array literals, commas aren’t comma operators, but part of the list syntax.

--