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:
```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









Walter Bright <newshound2 digitalmars.com> 