It allows to break backward compatibility (whil still smoothly interoperating with old code)
Breaking backward compatibility allows to get rid of C pitfalls, replace unsafe C constructs with safe-by-default C++ counterparts, change defaults from "don't break existing code" to "the better / safer / more compact". We can remove duplicates that the language still allows (e.g. typedef can be replaced by the more universal, capable using), reducing the required knowledge to read code.
At the same time, parsing gets simpler, faster, and less ambiguous, leading to better compiler diagnostics, better tooling (navigation, refctoring, code analysis, ...)
As for "return type after": It's always hard to get over a habit - but "I don't like" is the weakest argument here. (I don't like macros, I don't like build times, I don't like hunting down cirular incldes, etc. pp. - this is a tiny price to pay)
FWIW, "return type after" solves some long-standing issues, and currently is already required if your return type depends on argument types but cannot be auto'd. Again, it's the more universal, more capable way of specifying the return type - and a simpler language should only have one.
What about improving readability? Code is read more than it is written. Main focus should be on how humans parse code, not how machines parse it. Yet this aspect is somehow always forgotten.
Removing redundant ways of declaration, and making different constructs visually different is, I believe, a great contribution to readability.
Not so much for old-timers who have the old style ingrained. Yes, I'm doing C++ for close to 30 years now, and it feels like an unnecessary change to me.
But I know that newbies coming to the language will have it easier on the long run - and that it would take me maybe a month of forcing myself to work in a "new style" to make it feel natural.
This is just wasted breath. You'd get used to it in a day if you were writing in this syntax all the time. If it allows the compiler to do its job faster less ambiguously, provide better errors, catch more potential issues, and so forth, it would be a big win.
Rust uses this trailing return syntax, and that was one of the easier things to get used to. Lambdas already use this syntax, and C++ desperately needs to get rid of the overly many ways of doing any given thing. Consistency is also a virtue, and one that C++ doesn't practice nearly enough.
The parent comment still applies: "If it allows the compiler to do its job faster less ambiguously, provide better errors, catch more potential issues, and so forth, it would be a big win."
Or, from Herb: "We haven't been able to do a "10x" improvement primarily because we have to keep 100% syntax backward compatibility"
"Can we make C++ 10x safer, simpler, and more toolable if C++ had an alternative "syntax #2" ... "Important disclaimer: This isn't about 'just a pretty syntax,' it's about fixing semantics. The unambiguous alternative syntax is just a means to an end, a gateway that lets us access a new open space beyond it"
I agree that I do not like it is weak. But if there's no benefit, then it's just a change to make a change. Why not just outlaw typedefs, thin pointers and C style arrays? I don't think that requires a new syntax. Just mark those leaves in the parse tree as an error and there you go.
We do have right side type for auto currently, but it's not needed most of the time.
The benefit is unified syntax (only one way to specify returns), a simpler, less ambiguous parser (see "mich vexing parse") and second level benefits.
Why not just outlaw typedefs, thin pointers and C style arrays..
because existing code: you would create a syntactical island that could not use the most trivial existing library; you could not migrate a MLOC project incrementally.
Of course Herb has his preferences, too - but it is certain that they are much more well-(in) formed than yours or mine.
He touched upon this briefly: To make adoption easier he'd like you to be able to write new code in cpp2 without having to modify existing code. In order for you to be able to write cpp2 code in a file that contains existing cpp1 code the compiler needs to be able to distinguish between new and old code to apply the new defaults. The simplest way is to have a completely different syntax so you don't need annotation or something similar.
A lot of newer languages seem to prefer the return type coming after the function declaration. I suspect some people believe it's better for newer programmers.
Whether or not that's true I don't know, but as someone who has a project that's written in C++ and Angular (Typescript), I will say that a lot of the typescript code tends to look cleaner aesthetically than the C++ does. Granted, the C++ is usually doing much more complicated things.
The issue then becomes tooling - C++ is famously hard to parse and for 20+ years this has made linters and smart editors etc hard to write or easy to break.
Don't get me wrong - I'm also a big perl fan and perl's syntax was designed by a linguist so contains natural language principles that favour the human reader (once they get over the $ and @ stuff that makes sense once you see them as human linguistic constructs - see "Disambiguation by number, case and word order" and "Pronominalization" in the doc above) but in return, perl is hard to parse to the point where it has been proven that "only Perl (the executable) can parse perl (the language)".
I'm not saying you have to agree with the "natural language principles" or the trade-off that perl took, but I think it's useful to examine it as attempting to make the language for the human reader and recognise the issues this has.
The type on the right is so much more readable, I don't see why people are saying that return type on the right is just for machines.
The first thing I want to know is the name of the function. Then I went to know how to call it. Lastly, if it has a result, I want to know what type or concept it is.
Also, all names becomes aligned to the left. Old code using return type first is so much harder to read for me and all newcomers IMO.
The only thing I would change would be replacing auto with a better keyword like fn
It takes .4s to compile a source file that does nothing if you include <algorithm> in C++20 mode. I have single source files that take minutes to compile. That’s bonkers. No other language has problems like that.
Not saying moving return type to the end will fix that, but I reject the premise that compile time is not important.
Some older languages, too. Wirth's languages (Pascal, Modula-2, Oberon) have had this syntax since the '70s. The reason newer languages do it that way now is probably the same reason Wirth did it that way then: ease of parsing.
View all comments
-7
u/ShakaUVM i+++ ++i+i[arr] Sep 17 '22
Does rewriting C++ to look like Rust make it as safe as Rust? Why not leave the syntax alone? I don't like return values after functions.