[Issue 24649] Upper-bound-inclusive range foreach

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jul 8 16:54:17 UTC 2024


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

--- Comment #2 from Bolpat <qs.il.paperinik at 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.

--


More information about the Digitalmars-d-bugs mailing list