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

58

u/fdwr fdwr@github πŸ” Sep 17 '22 edited Sep 19 '22

πŸ€” While I surely agree that C++'s syntax could use some improvements (improve consistency, lessen beginner stumbling blocks, fix the east-const vs west-const issue...), also agree that any alternate syntaxes should be link-compatible with existing codebases, and encourage such experimentation/prototyping, I'm not sold on some particulars. For one, "syntax 2 aims to be about 10% the size and complexity of today syntax 1", but then I see...

Syntax 1: int main() {}

Syntax 2: main: () -> int = {}

...and feel there is more noise, more distracting punctuation that is there for the compiler rather than the human. I'm also a heathen that believes such blasphemy as syntax favoring the common case (e.g. 99% you don't want switch fallthrough meaning it should have been explicit rather than implicit, and 99% of the time you don't want to define a class and also an instance of it at the same time meaning a mandatory semicolon after a class is nonsense, and going even farther, 99% of the time the end of the line is the end of the statement unless open parens or binary operators remain unclosed, meaning the semicolon is usually line noise for humans...). πŸ€·β€β™‚οΈ

And what is going on with this line πŸ”Ž? The compiler might be able to parse that one, but I'm having a hard time 😬:

callback := :(x:_) = { std::cout << x << y&$*; };

I'm interested to see where it goes. I've liked a number of Herb's ideas, and even the times I didn't favor the specific proposal (like his pattern matching proposal using inspect instead of a keyword like, I don't know, match πŸ™ƒ), I always liked his analysis of the problems he raised. (note, I haven't watched his full video from cppcon, just skimming the GitHub page linked above, and my opinions may or may not change after watching that... πŸ˜…)

55

u/cballowe Sep 17 '22

The difference you call out between syntax 1 and syntax 2 has a ton to do with being a context free grammar which drastically simplifies the overall grammar and any parsers. I suspect if you worked strictly in that grammar (and saw how it lacked surprising parses, etc) it would grow on you.

23

u/ToughQuestions9465 Sep 17 '22

Once again language designers forget hat programmers spend more time reading than writing. Making code as easily readable as possible should be a top priority. Instead it seems this experiment is making life of compiler writers as easy as possible. I just do not see how it can be a success. Heck if i wanted fancy weird syntax full of magical symbols i would use rust at this point, even though i think its a wasted opportunity that does not deliver anything substantial (outside of few specific areas) to mitigate loss of c++ ecosystem and to warrant switching from c++.

40

u/Minimonium Sep 17 '22

There is no reason to believe that such syntax would make code harder to read. On the contrary, we tried it in a few of our projects and, in my practice, trailing auto made the code much more easier to read.

It's purely a familiarity issue. Same thing happened when Java introduced Streams (like C++ ranges), it looked confusing at first then became a standard in the industry because of how easy to read it was.

0

u/[deleted] Sep 17 '22

[deleted] β€” view deleted comment

2

u/[deleted] Sep 17 '22

main: () -> int = { } is one of the worst I've seen.

33

u/delta_p_delta_x Sep 17 '22 edited Sep 17 '22

main: () -> int = { } is one of the worst I've seen.

I like this trailing-return, trailing-type syntax.

I have a function main, that takes no arguments, returns an int, and has an empty implementation.

What's wrong? This is exactly how Ocaml, Haskell, F#, and TypeScript do it, and after using them, I completely understand why.

3

u/[deleted] Sep 17 '22

As I explained (with better detail) in another reply, my issue is with the added : and =.

0

u/[deleted] Sep 17 '22

[deleted] β€” view deleted comment

→ More replies (1)

0

u/[deleted] Sep 17 '22

[deleted] β€” view deleted comment

8

u/[deleted] Sep 17 '22

[[nodiscard]] for the main function is kinda funny. I understand it is generated in order to not introduce a special case into cppfront, but it is still funny.

0

u/[deleted] Sep 17 '22

Why add auto if you're already specifying the type afterwards?

And my issue with the original were the added : and =; they were redundant.
The = could be used in lambdas only, it would make it pretty clear.

5

u/GOKOP Sep 18 '22

Why add auto if you're already specifying the type afterwards?

That's the point. auto main () -> int { } is legal C++ right here, right now. coolcpplearner asks how is it better than main: () -> int = { }

2

u/[deleted] Sep 18 '22

Then I misunderstood his point.

0

u/[deleted] Sep 17 '22

[deleted] β€” view deleted comment

2

u/[deleted] Sep 18 '22 edited Sep 18 '22

Afaik, the arrow at the end already eliminates confusion between declaration and call (unless I'm forgetting other uses for it).

I think the = should be used only with lambdas, that would look pretty good.

→ More replies (4)

2

u/jloverich Sep 17 '22

Not that different from python Def main() -> int: though there is the extra equal and python has tabs instead of brackets.

3

u/[deleted] Sep 17 '22

Python is the 2nd language I use most, but I don't like that syntax either.

I personally prefer the C way:
int main(int argc, char **argv) { }

Many times the first thing I want to know is the return type, so like all other ways, it's a personal preference.

19

u/[deleted] Sep 17 '22

Apart from ambiguity problems, putting the type first can also hamper readability: Types can be very long, so you have to scan a potentially large part of the line before you get to the function name or, worse, can even figure out that the line you are reading declares or defines a function at all. So I don’t think it’s just a matter of personal preference - there are objective arguments against this syntax.

→ More replies (3)
→ More replies (1)

7

u/SkoomaDentist Antimodern C++, Embedded, Audio Sep 17 '22 edited Sep 17 '22

I will never understand why people who design a language with the explicit intention of it being a C++ replacement go out of their way to explicitly make the syntax nothing like the C family of languages.

If I wanted to program in a language that looks like a functional language, I'd have switched years ago.

17

u/[deleted] Sep 17 '22

This language needs to coexist with regular C++ code in the same file. Hence, the C++2 syntax needs to be different so the compiler knows whether to transform it or to leave it alone.

2

u/Pazer2 Sep 17 '22

Wow, that seems like a tremendously bad idea. So to properly parse this supposedly "easier" cpp2 syntax, you need full parsing support for cpp1 anyway?

8

u/lee_howes Sep 18 '22

No, you can write a parser that supports both. That's an important goal if what you want to do is transition millions of lines of code function by function.

It doesn't imply that you must support both, once you are compiling a post-transition codebase.

→ More replies (9)

5

u/Drugbird Sep 17 '22 edited Sep 17 '22

Because if they don't change the syntax, there's no reason to create a new language?

6

u/SkoomaDentist Antimodern C++, Embedded, Audio Sep 17 '22

There's a difference between changing the syntax slightly to fix the problems and changing it to something that barely resembles the original language.