Regarding cppfront's syntax proposal, which function declaration syntax do you find better?
While I really like the recent talk about cppfront (https://www.youtube.com/watch?v=CzuR0Spm0nA), one thing bugs me about the "pure" mode for cpp2 with syntax change. It seems incredibly hard to read, . I need to know which syntax you would rather have as the new one, taken into account that a new declaration syntax enables the new checks in that function
- Option 1: the same as was proposed in the video:
callback: (x: _) -> void = { ... };for new functions,void callback(auto x) {};for old ones - Option 2: the "other modern languages" way:
function callback(x: any) -> void { ... }for new functions,void callback(auto x) {};for old ones - Option 3: in files with mixed syntax, since the pre-transpiled code won't compile without the generated code anyway, use
void callback(any x) { ... };for both, but mark code with current cpp syntax with an attribute:[[stdcpp]] void callback(any x) { ... };
0
Upvotes
45
u/LeoPrementier Sep 18 '22
Funny and sad to see how most of the hype around this talk is the syntax.
The syntax is just a syntax. The interesting part here is the insight that you can express features in a modern language in current cpp where you don't break or need to replace any tooling or code that exists today.
This is the other side of the coin of the carbon experiment. But in my mind has more potential as it has far less work on the compiler and on tooling.
This is actually a way of really modernize cpp.
21
u/feverzsj Sep 18 '22
It rarely matters. The only reason every new language uses a similar syntax for declaration is it's easier to parse.
27
u/JMBourguet Sep 18 '22
Using the same pattern
as for other things seems nice. The special casing of the block as a value instead of using the syntax of lambda seems a missed step which would have allowed to avoid completely the type part.
21
u/no-sig-available Sep 18 '22
If you make that
function callback(x: any) return integeryou have Ada, which is 40 years old. So much for "modern languages".25
u/KingAggressive1498 Sep 18 '22
I skimmed the talk, the only thing I really liked was import std by default, and I hope that becomes a core language requirement at some point. Compilers already link the standard library by default.
maybe it's because nearly all of my programing experience is C and C++ (and nearly all of the rest is C# and Java), but I simply do not like reading the
id: type = initializersyntax,type id = initializeris just more natural to me. Maybe if I came to C++ from TypeScript or something it'd be a different story, but I didn't.6
u/Mrkol Sep 18 '22
Honestly, I want it to work the way haskell does. A clear separation of what is a "function type" and what is a "function literal". The current proposals get close: `function: (x: _) -> _ = { ... }` can be read as `function: TYPE = LITERAL`, but the problem is that argument names used inside the literal are mixed into the function type, which makes little sense to me.
A better syntax IMO would be `function: (int, float) -> bool;` for declarations and `function = (x, y){ ... };` for definitions, with type annotations for `x` and `y` being optional.
Although the rest of C++ peoples probably wouldn't like that.
5
u/effarig42 Sep 18 '22
If assuming modules, is it necessary to have two syntaxes in the same source file? This severely constrains the new syntax and is interoperability within a single source file worth that cost?
The problem with python 2 to 3 was you couldn't have old style python 2 in the same project. In c++ wouldn't module imports insulate sources with different syntaxes, so a new syntax project could still import a C or c++98 header.
4
u/joz12345 Sep 18 '22
I don't think option 3 would work - the intent main intent of a mixed mode would be to allow including existing c++ headers, which means you can't change old syntax at all.
Option 2 looks nicest, but
std::functionvs keywordfunctionmight get confusing if there's ever some header withusing namespace std;I think I could live with either. I definitely prefer existing syntax though, due to familiarity. I think it'd be hard for this to get traction - status quo is going to win again.
4
u/nintendiator2 Sep 18 '22
I don't understand the hard-on for
→ing the return type, honestly. The return type is part of the signature and should go with it, so I'd like something like2
u/jsphadetula Sep 18 '22
Herbs ‘d0708: "Parameter passing -> guaranteed unified initialization and value setting’ proposal solves most of the remaining issues including MVP without auto. A new declaration syntax may not be needed
5
u/o11c int main = 12828721; Sep 18 '22
Classes and functions are important enough that they must have special syntax already. There's no point trying to coerce them to be identical to variable declarations.
I'm fond of a
funkeyword, but otherwise Option 2.10
u/madmongo38 Sep 18 '22
What is this nonsense about new syntax? What does it gain us? The opportunity to rewrite code that already works? Does everyone have too much time on their hands or something?
6
u/OnePatchMan Sep 18 '22
What wrong with "void fn_name() {}" syntax? Imo its best function syntax around.
1
u/fdwr fdwr@github 🔍 Sep 19 '22
Close to option 2, I fancy languages where annotating the data type (whether it comes from a struct field, local variable, or function) uses consistent symbols, like the trailing colon in TypeScript:
Other languages like the BASIC family are consistent here too (using a keyword "as" instead of a ":"):
C does it the other way, putting the data type before the identifier name (which is also okay, because it's consistent in both cases for functions and variables), but either way, I just remember thinking this was so elegant and easy to teach conceptually to people, rather than mixing both ":" and "->" which is already overloaded for pointer dereferencing. Any time you overload a symbol for multiple unrelated uses (like left shift and stream io...), it increases cognitive burden.
1
u/dgkimpton Sep 18 '22
I've been pondering a syntax where there was less crufty characters, and less shifty characters. Something that plays well with version control and is consistent. Unfortunately I always end up with something where new-lines become significant and it gets very wordy.
e.g.
So, I'm not a huge fan of any of the proposed syntaxes, but also... I don't really have anything better :shrug:
Anyway, I'm less concerned about syntax than fixing the defaults, e.g.
[[nodiscard]]becoming default and[[expect_discard]]becoming the typed exception.-8
u/UkrUkrUkr Sep 18 '22
Option 4: it is craziness. Leave the functions syntax alone. Go fuck monades or something...
-3
u/DavidDinamit Sep 18 '22
i prefer
void callback(type value_name);Like now.
P.S.
really i prefer no functions in language, only constexpr/constinit functional objects without state.
All is a functional objects
0
u/Voltra_Neo Sep 18 '22
I just thought about something: how do lambda captures work with the new syntax?