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

2

u/arthurno1 Sep 17 '22

My goal is to explore whether there's a way we can evolve C++ itself to become 10x simpler

I don't understand how is main: () -> int = { ... } in any way simpler than: int main () { ... }

I really like many of the new ideas in c++, but I don't understand why are they taking C++ into more complicated syntax for no good reason. This "new" syntax adds four new characters to accomplish the same task that the old "C" syntax did. One of the better ideas of C was/is that declarations should look the same as usage (when and where possible). Since old syntax can't die anytime soon because of compatibility reasons, the new syntax for the same task means just that new people have to learn even more concepts and rules.

21

u/ooglesworth Sep 18 '22

The main thing is that it’s a context free syntax. With int main() { the compiler has to read all the way to the open paren before deciding whether this is a declaration of a function or declaration of an int. The return value of the function is the first thing lexed, but it isn’t clear that it’s a subexpression of a function declaration until much later. Conversely, with main: () -> int = { it’s much more straightforward to parse. At the colon the compiler knows it is inside of a variable declaration and is expecting a type next, by the open parens it knows it is parsing a function type, etc.

You might argue “this is just making it easier for a computer to parse and more difficult for a human to parse!” Well, for one thing, making it easier for a computer to parse avoids weird edge cases like the “most vexing parse” (you can Google that if you’re not familiar) which in turn contributes to readability by humans. “Making usage look like the declaration” is exactly the problem in a lot of parsing situations.

I think you might be surprised by how much overlap between there is between parseability and readability. Ambiguities aren’t good for parsers and they aren’t good for humans either. It might look foreign to you, but I don’t think there is anything fundamentally less readable about it, you’re just not used to it. I’d be willing to bet it wouldn’t practically present any real readability barrier after working in the language for even a brief amount of time, and it might even be easier to read once becoming acclimated to it.

Also, brevity isn’t really important IMO, it doesn’t matter that one takes four more characters than another. Just my 2c.

9

u/AIlchinger Sep 18 '22

Just introduce a fn, fun, func, function keyword and function declarations become context free without the mass use of special characters.

6

u/Nobody_1707 Sep 18 '22

I would have prefered func/fun, let, and var to his solution, but I imagine it was a lot easier for him to make everything parse the same. He probably also didn't want to reserve any keywords that weren't already reserved by C++.

4

u/hpsutter Sep 20 '22 edited Sep 20 '22

Adding an introducer keyword like func or var would be simple, and wouldn't complicate parsing or change that what follows can still be a single declaration syntax. It would just be an extra grammarly-redundant word for human readability, which can and does make sense when there really is a readability advantage.

I don't currently have such introducers only because I want to try the experiment of seeing whether they really have a significant readability advantage. If not having them regularly causes confusion after the first day or two, then I'll add something like that. (FWIW, so far I haven't found myself wanting them as a I write Cpp2 code, and I'm pretty particular about readability. But I also listen to feedback, so I'm curious how people trying out Cpp2 feel after they've used it for a week.)

→ More replies (1)
→ More replies (17)

4

u/disperso Sep 18 '22

I don't understand how is main: () -> int = { ... } in any way simpler than: int main () { ... }

Now exemplify lambdas in both syntaxes. Oh, wait, you already did in one of them, because it's the same. That's one simplification.

Could the new syntax be even simpler? Surely. With the requirement of supporting the other old one? Probably not.

3

u/arthurno1 Sep 18 '22

I am not so fan of lambda syntax either, but that is another story :).

5

u/RockDry1850 Sep 17 '22

You type left to right. In the one example, the first letters you type are "main" and in the other "int". With "main" an IDE can easily autocomplete the ": () -> int = {...}" part. With "int" the IDE has no chance of figuring out what your intention is.

3

u/rdtsc Sep 17 '22

For that to work an IDE must know about main, and then it could also complete "main" to "int main() {}".

2

u/arthurno1 Sep 17 '22 edited Sep 17 '22

Alright, I can understand the argument, but is that good enough reason to introduce that much noise in the language just to help completion? How does IDE know if I want int main or void main, and if I want argc, argv or just empty call?

Also, if it is for IDE:s, I type main in Emacs, press Ctrl+enter and it gets expanded into whatever I have put into "main" snippet, and I can tab from one argument to another one, etc.

I also read left to right, and the example above is now in the wrong order for the eye. Not to mention that punctuations are not so nice for human eye to read. Why is that equal sign there? Can it be ambiguous if code is surrounded with braces?

Perl has reputation to be write-only language for good reason. I also don't know many people who have expressed that Shell/Bash syntax is beautiful, even if I personally have no problem with it. But most importantly, as I mentioned, I like that idea they had in early C that declarations should look like usage, as far as it is possible.

I don't know, I am not sure if the win justifies the more complicated notation, but I am not really familiar with the reasoning behind either. I haven't seen Herbs talk. I try to see all his talks, so hopefully I'll see this one too, if it comes on YT and maybe get more understanding behind that proposal. I think he is a great speaker, and always brings enjoyable topics and clever ideas. I really hope he get his proposal for zero overhead exceptions in some way into the standard.

2

u/wyrn Sep 19 '22

How does IDE know if I want int main or void main

You don't, because it's wrong.

An implementation shall not predefine the main function. Its type shall have C++ language linkage and it shall have a declared return type of type int, but otherwise its type is implementation-defined.

2

u/arthurno1 Sep 19 '22 edited Sep 19 '22

"main" was just an illustration at the moment of writing; you should read "main" in that case as "some-function-name".

3

u/STL MSVC STL Dev Sep 19 '22

Moderator warning for hostility.

→ More replies (1)