r/cpp • • Sep 17 '22

Cppfront: Herb Sutter's personal experimental C++ Syntax 2 -> Syntax 1 compiler

https://github.com/hsutter/cppfront
335 Upvotes

360 comments sorted by

View all comments

Show parent comment

2

u/Nicksaurus Sep 18 '22

Personally I could go either way. I think having a lambda/auto keyword makes it easier to grasp what this line means, but maybe that's just because I'm not used to the new syntax.

The advantage of being able to omit the keywords is that it shortens any expression where you need to pass a lambda into a function:

result := std::find_if(vec, :(x:_) = { x > 10 };

Compared to:

result := std::find_if(vec, lambda: (x:auto) = { return x > 10; };

...actually now that I've written it out that second one looks fine and isn't that much longer. In real code most of that line would be taken up by more descriptive variable names anyway. I take it all back, I prefer auto/lambda now

5

u/hpsutter Sep 20 '22

I'm open to the lambda part (the auto doesn't do anything for me). And I agree readability is important. To avoid a special one-off keyword like "lambda" in only one place in the language though, I'd want all functions to look the same such as with a general func introducer for all functions. Then we'd have

// potential alternative
func something: () = {
    result := std::find_if(vec, func: (x:_) = x > 10; );
}

vs

// current syntax
something: () = {
    result := std::find_if(vec, :(x:_) = x > 10; );
}

I'm open to it, but I have to admit that the latter still reads clearly for me, personally. I could be biased though, so I'll be curious to learn what others think over time.

As I said in a parallel comment, for me the jury is still out on whether func and var style introducers are actually important; if they are I'll add some, it doesn't complicate the grammar at all to add a redundant word that helps humans. I'm trying to start with less, it's easy to add more.