News for hackers. Stuff that matters.
Re:C++20 Improved the For-Loop Syntax by WalterBright on Tuesday July 14, 2026 @09:35PM (#48915184)
C++ should copy D's elegance:

    import std.stdio;

    string[9] vec = [ 
      "the", "quick", "brown", "fox", 
      "jumped", "over", "the", "lazy", "dog" 
    ];

    void main()
    {
      foreach (i, s; vec)
        writeln(i, ": ", s);
    }
Re:C++20 Improved the For-Loop Syntax by entrope on Tuesday July 14, 2026 @10:25PM (#48915523)
Go is similar:

  package main

  import "fmt"

  var vec = []string{"the", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"}

  func main() {
    for i, s := range vec {
      fmt.Printf("%d: %s\n", i, s)
    }
  }
Using `fmt.Println(i, ":", s)` inserts spaces on both sides of the colon, so the output is not quite right.

I find Go's `range` syntax easier to read than D's `(i, s; vec)` -- although generator functions that work with `range` have unaesthetic return types.

Re:C++20 Improved the For-Loop Syntax by WalterBright on Tuesday July 14, 2026 @10:29PM (#48915550)
D has several usages for `foreach` depending on the number and type of its arguments. In this case, it sees that `vec` is an array, and so constructs a loop over the array. If `vec` was a range, the loop will be constructed as a loop over the array.

It's nice because it makes the syntax for arrays and ranges interchangeable, making for easy refactoring.

Re:C++20 Improved the For-Loop Syntax by entrope on Wednesday July 15, 2026 @07:28AM (#48919209)
Go is similar: `for/range` has always worked on arrays, slices, maps, strings and channels, and they extended it to integers in version 1.22 and to user-defined generators in version 1.23. It generates one or two outputs depending on the type of the RHS, but one can assign the index value to _ to ignore it. Among standard types, only channels and integers generate one output; containers (including strings) give an index or key and a value.
Re:C++20 Improved the For-Loop Syntax by xigoi on Wednesday July 15, 2026 @09:35AM (#48920637)
I find Go’s version worse in that it does not allow you to elide the index (which is not needed most of the time). You have to write `for _, s := range vec`, as `for s := range vec` makes s the index.
Re:C++20 Improved the For-Loop Syntax by WalterBright on Wednesday July 15, 2026 @01:39PM (#48924429)
To clarify,

    foreach (s; vec)
works if you don't need the index. To be able to modify s in place:

    foreach (ref s; vec) s = "replacement";
Re:C++20 Improved the For-Loop Syntax by xigoi on Wednesday July 15, 2026 @05:34PM (#48927345)
I was talking about Go. I know you don’t need the index in D.
Re:C++20 Improved the For-Loop Syntax by WalterBright on Tuesday July 14, 2026 @09:47PM (#48915272)
Although D is a strongly typed language, it is very good at type inference. The `s` is inferred as `string`, and `i` as `size_t`.
Re:C++20 Improved the For-Loop Syntax by hdjrudni on Wednesday July 15, 2026 @01:42AM (#48916687)
I don't like that semi-colon. AFAICT `i, s;` is not an independent statement. `i, s: vec` would have been fine, but `;` ?!?! madness.
Re:C++20 Improved the For-Loop Syntax by acehreli on Wednesday July 15, 2026 @04:01PM (#48926327)
Well, others might have said `i, s:` is not an independent label.
Re:C++20 Improved the For-Loop Syntax by pjmlp on Wednesday July 15, 2026 @05:21AM (#48918228)
C++23 example,

    import std;

    using namespace std;

    int main() {
        vector vec = { 
            "the", "quick", "brown", "fox", 
            "jumped", "over", "the", "lazy", "dog" 
        };

        for (auto&& [i, it] : vec | std::views::enumerate)
            println ("{}:{}", ++i, it);
    }
As I keep mentioning, the features being copied, slowly erode D's relevance.
Re:C++20 Improved the For-Loop Syntax by WalterBright on Wednesday July 15, 2026 @01:37PM (#48924383)
If you prefer writing:

    for (auto&& [i, it] : vec | std::views::enumerate)
        println ("{}:{}", ++i, it);
instead of:

    foreach (i, s; vec)
        writeln(i, ": ", s);
well, what can I say?
Re:C++20 Improved the For-Loop Syntax by acehreli on Wednesday July 15, 2026 @03:58PM (#48926295)
We may first need to go over what "elegant" means I guess. :)
Re:C++20 Improved the For-Loop Syntax by pjmlp on Wednesday July 15, 2026 @05:18PM (#48927126)
There is elegant when writing code by hand, and when reviewing code by AI agents.
Re:C++20 Improved the For-Loop Syntax by pjmlp on Wednesday July 15, 2026 @05:17PM (#48927103)
I prefer to use the option with better market share.

I am not counting characters, especially when AI completes typing for me.