digitalmars.D - C++ still doesn't have good loop syntax
- Walter Bright (22/22) Jul 14 ```d
- matheus (7/8) Jul 14 In C++23 still ugly:
- Walter Bright (4/16) Jul 14 C++ keeps circling around doing a better syntax, but never quite close t...
- Meta (6/28) Jul 15 Hoping we can someday make the D example even better with static
- H. S. Teoh (6/12) Jul 15 Didn't OpenD already implement this feature?
- Meta (2/12) Jul 15 No idea, I haven't used OpenD.
- monkyyy (2/3) Jul 15 your going to make adr sad ;__;
- Adam D. Ruppe (8/12) Jul 15 Yes, we merged the PR upstream rejected.
- Meta (2/15) Jul 15 Oh, very nice.
- user1234 (5/18) Jul 15 IMO that's a workaround. Imagine that array literals are static
- Kapendev (7/29) Jul 15 The other solution I can think of that Nim is doing something
- user1234 (7/23) Jul 15 Yes sure, in the great tradition of D we could also imagine a
- Kapendev (6/31) Jul 15 This one is OK too, just paying for a template there.
- Kapendev (3/17) Jul 15 Ah, it's not the thing from the standard library. Anyway, same
- user1234 (10/28) Jul 15 yeah you got it finally, it's about checking that during
- H. S. Teoh (11/27) Jul 15 [...]
- Dennis (2/4) Jul 15 https://github.com/dlang/dmd/pull/22745
- Meta (2/3) Jul 15 π₯π₯π₯
- Quirin Schroll (3/37) Jul 21 Dβs going to have it soon:
- Mindy Batek (0xEAB) (6/10) Aug 09 PHP:
- Mindy Batek (0xEAB) (4/9) Aug 09 With this syntax, I've never had to wonder which variable was the
- Walter Bright (6/9) Aug 09 It's easy to remember. Nobody says value-key. It's always key-value!
- Mindy Batek (0xEAB) (4/6) Aug 10 Chapeau!
- Forum User (25/27) Aug 11 Except in the case of opIndexAssign:
```d
import std.stdio;
string[9] vec = [
"the", "quick", "brown", "fox",
"jumped", "over", "the", "lazy", "dog"
];
void main()
{
foreach (i, s; vec)
writeln(i, ": ", s);
}
```
https://news.ycombinator.com/item?id=48915184
```C++
vector<string> vec = {
"the", "quick", "brown", "fox",
"jumped", "over", "the", "lazy", "dog"
};
for (int i=0; auto&& it: vec)
cout << (++i) << ": " << it << endl;
```
https://lzon.ca/posts/tips/cpp-for-range-init/
Jul 14
On Wednesday, 15 July 2026 at 01:43:56 UTC, Walter Bright wrote:...In C++23 still ugly: auto enumerated_vec = std::views::enumerate(vec); for (auto [i, s] : enumerated_vec ) { std::cout << i << ": " << s << "\n"; } Matheus.
Jul 14
C++ keeps circling around doing a better syntax, but never quite close the deal. This means that there will be another iteration of this, but the various previous incarnations will have to be supported forever. On 7/14/2026 7:45 PM, matheus wrote:On Wednesday, 15 July 2026 at 01:43:56 UTC, Walter Bright wrote:...In C++23 still ugly: auto enumerated_vec = std::views::enumerate(vec); for (auto [i, s] : enumerated_vec ) { Β Β Β Β Β Β Β std::cout << i << ": " << s << "\n"; Β Β Β } Matheus.
Jul 14
On Wednesday, 15 July 2026 at 01:43:56 UTC, Walter Bright wrote:
```d
import std.stdio;
string[9] vec = [
"the", "quick", "brown", "fox",
"jumped", "over", "the", "lazy", "dog"
];
void main()
{
foreach (i, s; vec)
writeln(i, ": ", s);
}
```
https://news.ycombinator.com/item?id=48915184
```C++
vector<string> vec = {
"the", "quick", "brown", "fox",
"jumped", "over", "the", "lazy", "dog"
};
for (int i=0; auto&& it: vec)
cout << (++i) << ": " << it << endl;
```
https://lzon.ca/posts/tips/cpp-for-range-init/
Hoping we can someday make the D example even better with static
static array length inference π
```d
string[$] vec = [ ... ]; // or some equivalent syntax
```
Jul 15
On Wed, Jul 15, 2026 at 03:00:13PM +0000, Meta via Digitalmars-d wrote: [...]Hoping we can someday make the D example even better with static static array length inference π ```d string[$] vec = [ ... ]; // or some equivalent syntax ```Didn't OpenD already implement this feature? T -- IBM = I Blame Microsoft
Jul 15
On Wednesday, 15 July 2026 at 15:28:54 UTC, H. S. Teoh wrote:On Wed, Jul 15, 2026 at 03:00:13PM +0000, Meta via Digitalmars-d wrote: [...]No idea, I haven't used OpenD.Hoping we can someday make the D example even better with static static array length inference π ```d string[$] vec = [ ... ]; // or some equivalent syntax ```Didn't OpenD already implement this feature? T
Jul 15
On Wednesday, 15 July 2026 at 16:42:20 UTC, Meta wrote:No idea, I haven't used OpenD.your going to make adr sad ;__;
Jul 15
On Wednesday, 15 July 2026 at 15:28:54 UTC, H. S. Teoh wrote:Yes, we merged the PR upstream rejected. void main() { string[$] a = ["a", "b"]; pragma(msg, typeof(a)); } $ opend -o- sa string[2]```d string[$] vec = [ ... ]; // or some equivalent syntax ```Didn't OpenD already implement this feature?
Jul 15
On Wednesday, 15 July 2026 at 17:02:10 UTC, Adam D. Ruppe wrote:On Wednesday, 15 July 2026 at 15:28:54 UTC, H. S. Teoh wrote:Oh, very nice.Yes, we merged the PR upstream rejected. void main() { string[$] a = ["a", "b"]; pragma(msg, typeof(a)); } $ opend -o- sa string[2]```d string[$] vec = [ ... ]; // or some equivalent syntax ```Didn't OpenD already implement this feature?
Jul 15
On Wednesday, 15 July 2026 at 17:02:10 UTC, Adam D. Ruppe wrote:On Wednesday, 15 July 2026 at 15:28:54 UTC, H. S. Teoh wrote:IMO that's a workaround. Imagine that array literals are static arrays by default, then no problem. Eventually you can turn them into dynamic arrays using implicit convertion / type inference. However truth is that D cannot change such a thing given its age.Yes, we merged the PR upstream rejected. void main() { string[$] a = ["a", "b"]; pragma(msg, typeof(a)); } $ opend -o- sa string[2]```d string[$] vec = [ ... ]; // or some equivalent syntax ```Didn't OpenD already implement this feature?
Jul 15
On Wednesday, 15 July 2026 at 19:06:50 UTC, user1234 wrote:On Wednesday, 15 July 2026 at 17:02:10 UTC, Adam D. Ruppe wrote:The other solution I can think of that Nim is doing something like this: ```d auto static_array = $[1, 2, 3]; // int[3] ``` You can replace the `$` with any character, not important.On Wednesday, 15 July 2026 at 15:28:54 UTC, H. S. Teoh wrote:IMO that's a workaround. Imagine that array literals are static arrays by default, then no problem. Eventually you can turn them into dynamic arrays using implicit convertion / type inference. However truth is that D cannot change such a thing given its age.Yes, we merged the PR upstream rejected. void main() { string[$] a = ["a", "b"]; pragma(msg, typeof(a)); } $ opend -o- sa string[2]```d string[$] vec = [ ... ]; // or some equivalent syntax ```Didn't OpenD already implement this feature?
Jul 15
On Wednesday, 15 July 2026 at 19:23:50 UTC, Kapendev wrote:On Wednesday, 15 July 2026 at 19:06:50 UTC, user1234 wrote:Yes sure, in the great tradition of D we could also imagine a DotExp property, i.e ```d auto static_array = [1, 2, 3].staticarrayof; ``` πOn Wednesday, 15 July 2026 at 17:02:10 UTC, Adam D. Ruppe wrote:The other solution I can think of that Nim is doing something like this: ```d auto static_array = $[1, 2, 3]; // int[3] ``` You can replace the `$` with any character, not important.[...]IMO that's a workaround. Imagine that array literals are static arrays by default, then no problem. Eventually you can turn them into dynamic arrays using implicit convertion / type inference. However truth is that D cannot change such a thing given its age.
Jul 15
On Wednesday, 15 July 2026 at 19:36:47 UTC, user1234 wrote:On Wednesday, 15 July 2026 at 19:23:50 UTC, Kapendev wrote:This one is OK too, just paying for a template there. The good thing about it is that it's not a language change. Now, the choice between what `[item1, item2]` should be by default is not important in my opinion, but I get it being weird or annoying for some.On Wednesday, 15 July 2026 at 19:06:50 UTC, user1234 wrote:Yes sure, in the great tradition of D we could also imagine a DotExp property, i.e ```d auto static_array = [1, 2, 3].staticarrayof; ``` πOn Wednesday, 15 July 2026 at 17:02:10 UTC, Adam D. Ruppe wrote:The other solution I can think of that Nim is doing something like this: ```d auto static_array = $[1, 2, 3]; // int[3] ``` You can replace the `$` with any character, not important.[...]IMO that's a workaround. Imagine that array literals are static arrays by default, then no problem. Eventually you can turn them into dynamic arrays using implicit convertion / type inference. However truth is that D cannot change such a thing given its age.
Jul 15
On Wednesday, 15 July 2026 at 19:52:14 UTC, Kapendev wrote:Ah, it's not the thing from the standard library. Anyway, same thing.Yes sure, in the great tradition of D we could also imagine a DotExp property, i.e ```d auto static_array = [1, 2, 3].staticarrayof; ``` πThis one is OK too, just paying for a template there. The good thing about it is that it's not a language change. Now, the choice between what `[item1, item2]` should be by default is not important in my opinion, but I get it being weird or annoying for some.
Jul 15
On Wednesday, 15 July 2026 at 19:53:40 UTC, Kapendev wrote:On Wednesday, 15 July 2026 at 19:52:14 UTC, Kapendev wrote:yeah you got it finally, it's about checking that during compilation directly. if the RHS is an Ident and if that ident is `ident(staticarrayof)`, then let's type the exp as a static array instead. anyway, life is so. That would add some complexity tho. The goal of a language (compared to another, older) is to avoid those special case actually.Ah, it's not the thing from the standard library. Anyway, same thing.Yes sure, in the great tradition of D we could also imagine a DotExp property, i.e ```d auto static_array = [1, 2, 3].staticarrayof; ``` πThis one is OK too, just paying for a template there. The good thing about it is that it's not a language change. Now, the choice between what `[item1, item2]` should be by default is not important in my opinion, but I get it being weird or annoying for some.
Jul 15
On Wed, Jul 15, 2026 at 07:36:47PM +0000, user1234 via Digitalmars-d wrote:On Wednesday, 15 July 2026 at 19:23:50 UTC, Kapendev wrote:[...]On Wednesday, 15 July 2026 at 19:06:50 UTC, user1234 wrote:[...] Umm... we already have std.array.staticArray. This works today: ``` import std.array; auto static_array = [1, 2, 3].staticArray; ``` T -- This is not a sentence.The other solution I can think of that Nim is doing something like this: ```d auto static_array = $[1, 2, 3]; // int[3] ``` You can replace the `$` with any character, not important.Yes sure, in the great tradition of D we could also imagine a DotExp property, i.e ```d auto static_array = [1, 2, 3].staticarrayof; ```
Jul 15
On Wednesday, 15 July 2026 at 15:00:13 UTC, Meta wrote:Hoping we can someday make the D example even better with static static array length inference πhttps://github.com/dlang/dmd/pull/22745
Jul 15
On Wednesday, 15 July 2026 at 19:53:21 UTC, Dennis wrote:https://github.com/dlang/dmd/pull/22745π₯π₯π₯
Jul 15
On Wednesday, 15 July 2026 at 15:00:13 UTC, Meta wrote:On Wednesday, 15 July 2026 at 01:43:56 UTC, Walter Bright wrote:Dβs going to have it soon: https://dlang.org/changelog/pending.html#dmd.sarr-length-infer```d import std.stdio; string[9] vec = [ "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" ]; void main() { foreach (i, s; vec) writeln(i, ": ", s); } ``` https://news.ycombinator.com/item?id=48915184 ```C++ vector<string> vec = { "the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog" }; for (int i=0; auto&& it: vec) cout << (++i) << ": " << it << endl; ``` https://lzon.ca/posts/tips/cpp-for-range-init/Hoping we can someday make the D example even better with static static array length inference π ```d string[$] vec = [ ... ]; // or some equivalent syntax ```
Jul 21
On Wednesday, 15 July 2026 at 01:43:56 UTC, Walter Bright wrote:
```d
foreach (i, s; vec)
writeln(i, ": ", s);
```
PHP:
```php
foreach ($vec as $i => $s)
echo $i, ": ", $s;
```
Aug 09
On Sunday, 9 August 2026 at 17:22:07 UTC, Mindy Batek (0xEAB)
wrote:
PHP:
```php
foreach ($vec as $i => $s)
echo $i, ": ", $s;
```
With this syntax, I've never had to wonder which variable was the
key and which was the value.
Aug 09
On 8/9/2026 10:25 AM, Mindy Batek (0xEAB) wrote:foreach ($vec as $i => $s) With this syntax, I've never had to wonder which variable was the key and which was the value.It's easy to remember. Nobody says value-key. It's always key-value! ```d foreach (i, s; vec) ``` Much more compact, too.
Aug 09
On Monday, 10 August 2026 at 00:08:04 UTC, Walter Bright wrote:It's easy to remember. Nobody says value-key. It's always key-value!Chapeau! After all, providing value to developers is key for a programming languageβ¦
Aug 10
On Monday, 10 August 2026 at 00:08:04 UTC, Walter Bright wrote:[...] Nobody says value-key. It's always key-value!Except in the case of opIndexAssign: struct S (T) { size_t siz; T [] t; disable this (); this (size_t siz) { t = new T [siz]; this.siz = siz; } auto opIndexAssign (size_t i, T v) { return t [i] = v; } } void main () { auto s = S!double (4); foreach (i; 0 .. s.siz) s [i] = 1000; } core.exception.ArrayIndexError soia.d(16): index [1000] is out of bounds for array of length 4
Aug 11









Walter Bright <newshound2 digitalmars.com> 