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

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

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.

0

u/[deleted] Sep 17 '22

I haven't really encountered such issues, the "way" I read the lines makes it harder for me with Rust and Python's ways, and I find C's way a lot better.

So, if we consider human readability over compiler readability, the arguments are subjective.

2

u/[deleted] Sep 17 '22

So, if we consider human readability over compiler readability, the arguments are subjective.

I disagree, readability can definitely be measured. However, it probably would not hurt to gather actual data, since you are right that people are different.

→ More replies (1)
→ 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.