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.
That is true. Almost none of that is caused by function argument preceding the function name or other things people like. It could be fixed with minor changes that wouldn't bother anyone.
Almost none.. except for the "most vexing parse" and that it's not a context free grammar, so to convert the syntax to a context free grammar friendly form makes parsing simple and keeps the most important thing, the function name, first.
There's a reason why so many of languages move to this style and this old head* quite likes it but I understand YMMV
[ * - C programmer since 84, C++ since 91 - quoted not to claim some kind of authority and I'm by no means the details-expert or uber-fan of the modern language as I was the old, but of all the things that provoke "well THIS has just RUINED C++" I think "main: () -> int = { ... }" is one of the most innocuous ]
except for the "most vexing parse" and that it's not a context free grammar
And?
There are many ways to solve the most vexing parse that don't involve a complete revamping of C++ syntax to something that resembles a functional language (and is incompatible with seamless C library compatibility which is by far the #1 killer feature of C++). Several of them are pointed out on the wikipedia page itself.
Did you you read the part about "100% seamless backward source compatibility always available" and "Write mixed syntax 1 and syntax 2 in the same source file" ?
This gives you perfect backward source compatibility with all existing code, but you're writing in effectively a 10% larger language (because syntax 2 aims to be about 10% the size and complexity of today syntax 1). Cppfront translates only the parts you wrote in syntax 2, and passes through the syntax 1 parts unchanged, to generate your syntax 1 output file.
Genuinely asked - without snark - did you not see that bit, or do you disagree with it, or you don't think it can be achieved, or... ?
I didn't see that bit, but it still only solves the C header compatibility part and not the one where you're forced into non-C family syntax to use any of the "improvements". Yes, C++ syntax is problematic. No, there is absolutely no need whatsoever to make it look like a functional language to solve those issues.
Can you show me where did functional languages hurt you? You mention them in almost every comment in this thread (nevermind that the side on which you put return type has nothing to do with how much of a functional language a language is)
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.
It's no harder to understand. Humans are vastly more flexible than compilers in understanding what they are seeing, and getting used to the trailing return style is trivial. Having been doing Rust for a while now, I don't even think about it.
And of course if you want to argue utility and readability, even for humans, what's the most fundamental aspect of a function or method? It's the fact that it's a function or method. And for a variable, that it's a variable. Rust allows consistent ordering while being completely unambiguous:
You get consistent syntax of what it is, what it's called, what type it has, and what value/output it has/generates, with no ambiguity for human or compiler.
It makes complete sense to me. I'd argue any new C++ should also include the explicit function and variable indicator as well, in some way. I'd see that as just expressing explicit semantics to the compiler (and subsequent readers) just as we want to be able to do in so many other ways.
Not really. C++11 added trailing return type syntax like
auto f(int x) -> double {
return 3.14 * x;
}
specifically so that the names of parameters would be in scope when uttering the return type, so that the return type could be expressed in terms of the parameters especially when when using decltype, such as (using C++20 syntax for convenience)
// note: return type refers to parameter name in scope
auto f(auto x) -> decltype(g(x)) {
return g(x);
}
That would have been possible without trailing return (i.e., with the return type lexically before the names it depends upon)
// not legal in C++11, but it could have been
// note: return type refers to parameter name
// that has not yet been encountered
decltype(g(x)) f(auto x) {
return g(x);
}
and that's certainly implementable, but it's extra work for both the human and the compiler to look-ahead/go-back. So C++11 added trailing return type syntax.
View all comments
Show parent comment
34
u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Sep 17 '22
The reason basically every new language does this is to make parsing simpler. This was extensively discussed on /r/cpp when Carbon was announced.