www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C++ still doesn't have good loop syntax

reply Walter Bright <newshound2 digitalmars.com> writes:
```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
next sibling parent reply matheus <matheus gmail.com> writes:
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
parent Walter Bright <newshound2 digitalmars.com> writes:
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
prev sibling parent reply Meta <jared771 gmail.com> writes:
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
next sibling parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
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
next sibling parent reply Meta <jared771 gmail.com> writes:
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: [...]
 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
No idea, I haven't used OpenD.
Jul 15
parent monkyyy <crazymonkyyy gmail.com> writes:
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
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 15 July 2026 at 15:28:54 UTC, H. S. Teoh wrote:
 ```d
 string[$] vec = [ ... ]; // or some equivalent syntax
 ```
Didn't OpenD already implement this feature?
Yes, we merged the PR upstream rejected. void main() { string[$] a = ["a", "b"]; pragma(msg, typeof(a)); } $ opend -o- sa string[2]
Jul 15
next sibling parent Meta <jared771 gmail.com> writes:
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:
 ```d
 string[$] vec = [ ... ]; // or some equivalent syntax
 ```
Didn't OpenD already implement this feature?
Yes, we merged the PR upstream rejected. void main() { string[$] a = ["a", "b"]; pragma(msg, typeof(a)); } $ opend -o- sa string[2]
Oh, very nice.
Jul 15
prev sibling parent reply user1234 <user1234 12.de> writes:
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:
 ```d
 string[$] vec = [ ... ]; // or some equivalent syntax
 ```
Didn't OpenD already implement this feature?
Yes, we merged the PR upstream rejected. void main() { string[$] a = ["a", "b"]; pragma(msg, typeof(a)); } $ opend -o- sa string[2]
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
parent reply Kapendev <alexandroskapretsos gmail.com> writes:
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:
 On Wednesday, 15 July 2026 at 15:28:54 UTC, H. S. Teoh wrote:
 ```d
 string[$] vec = [ ... ]; // or some equivalent syntax
 ```
Didn't OpenD already implement this feature?
Yes, we merged the PR upstream rejected. void main() { string[$] a = ["a", "b"]; pragma(msg, typeof(a)); } $ opend -o- sa string[2]
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.
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.
Jul 15
parent reply user1234 <user1234 12.de> writes:
On Wednesday, 15 July 2026 at 19:23:50 UTC, Kapendev wrote:
 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:
 [...]
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.
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
next sibling parent reply Kapendev <alexandroskapretsos gmail.com> writes:
On Wednesday, 15 July 2026 at 19:36:47 UTC, user1234 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:
 On Wednesday, 15 July 2026 at 17:02:10 UTC, Adam D. Ruppe 
 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.
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; ``` šŸ™ƒ
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
parent reply Kapendev <alexandroskapretsos gmail.com> writes:
On Wednesday, 15 July 2026 at 19:52:14 UTC, Kapendev 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;
 ```

 šŸ™ƒ
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.
Ah, it's not the thing from the standard library. Anyway, same thing.
Jul 15
parent user1234 <user1234 12.de> writes:
On Wednesday, 15 July 2026 at 19:53:40 UTC, Kapendev wrote:
 On Wednesday, 15 July 2026 at 19:52:14 UTC, Kapendev 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;
 ```

 šŸ™ƒ
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.
Ah, it's not the thing from the standard library. Anyway, same thing.
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.
Jul 15
prev sibling parent "H. S. Teoh" <hsteoh qfbox.info> writes:
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:
[...]
 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; ```
[...] 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.
Jul 15
prev sibling next sibling parent reply Dennis <dkorpel gmail.com> writes:
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
parent Meta <jared771 gmail.com> writes:
On Wednesday, 15 July 2026 at 19:53:21 UTC, Dennis wrote:
 https://github.com/dlang/dmd/pull/22745
šŸ”„šŸ”„šŸ”„
Jul 15
prev sibling parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
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
 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 ```
D’s going to have it soon: https://dlang.org/changelog/pending.html#dmd.sarr-length-infer
Jul 21