Live data from Hacker News

C++20 Improved the For-Loop Syntax

lzon.ca

81–89 of 89 posts

Re: C++20 Improved the For-Loop Syntax

#81
post #74
post #36

Earlier quoted context omitted.

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 wi…

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.

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

#82
post #59

Earlier quoted context omitted.

Some folks don't like iostreams, I keep using them, since Turbo C++ 1.0 for MS-DOS. Although I would probably use std::print in more modern compilers. D needs to work into its marketing, all key features that made it relevant back in 2011 with Andrei's book have now been copied even if badly, across all mainstream languages.

> Some folks don't like iostreams I thought it was ugly in 1987, and it hasn't improved with age. Then for templates made it worse. Then there's formatted I/O: void IOS_precision() { cout and I don't know if the problem with multithreading was resolved or not. > copied even if badly Any program can be written in any language. But why suffer?

That is always given as an example of how bad it is, yet I seldom have used any kind of such formatting since 1993.

iostreams hardly played a role in all C++ GUI frameworks and for object serialisation, precision flags were seldom used.

Re: C++20 Improved the For-Loop Syntax

#83
post #62

Earlier quoted context omitted.

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.

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?

We may first need to go over what "elegant" means I guess. :)

Re: C++20 Improved the For-Loop Syntax

#84

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); }

I don't like that semi-colon. AFAICT `i, s;` is not an independent statement. `i, s: vec` would have been fine, but `;` ?!?! madness.

Well, others might have said `i, s:` is not an independent label.

Re: C++20 Improved the For-Loop Syntax

#85
post #62

Earlier quoted context omitted.

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.

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?

I prefer to use the option with better market share.

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

Re: C++20 Improved the For-Loop Syntax

#86

Earlier quoted context omitted.

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?

We may first need to go over what "elegant" means I guess. :)

There is elegant when writing code by hand, and when reviewing code by AI agents.

Re: C++20 Improved the For-Loop Syntax

#87
post #74

Earlier quoted context omitted.

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.

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";

I was talking about Go. I know you don’t need the index in D.

Re: C++20 Improved the For-Loop Syntax

#88
post #5

Earlier quoted context omitted.

C++ is probably the only language that needs a strong style guide, to define a subset you are allowed to use in the project. Linus Torwalds famously said that subset is zero. You're not allowed to use C++ in Linux, a wise move. Here's Google's: https://google.github.io/styleguide/cppguide.html Search for "do not use" and you'll find plenty of hits.

And it's not because he's cranky or something, he was holding out for something worthy to possibly use instead of C. Turned out to be Rust.

> And it's not because he's cranky or something, he was holding out for something worthy to possibly use instead of C.

Good point.

> Turned out to be Rust.

He was evaluating C++ in the 90's, you make it sound like he recently decided to write Rust instead of C. The reality is that Rust is allowed in limited ways into _some parts_ of the kernel codebase. Rust got so much further than C++ ever did, that is true.

Re: C++20 Improved the For-Loop Syntax

#89

Earlier quoted context omitted.

Not really, I was well aware of if-with-init and I still found this quite confusing. That one actually shows what the variable is initialized to. This one doesn't.

> That one actually shows what the variable is initialized to. This one doesn't. reply Which variable? `i` is clearly initialized to `0` and `it` doesn't have an initializer per se... it starts at the first value of the container. It almost seems like an abuse to put 2 unrelated things in the `for(...)` but that's what they did.

[dead]
Post reply on HN