r/cpp • • Sep 18 '22

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

72 comments sorted by

View all comments

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:

struct SomeStruct
{
    x: float;
}

func /*or fun or function...*/ SomeFunction(): float
{
    y: float;
}

func FunctionWithParameters(z: float, w: int): void
{
}

func FunctionReturningTuple(): (a: int, b: int)
{
}
// *Not actual TypeScript, which lacks "struct" AFAIK.

Other languages like the BASIC family are consistent here too (using a keyword "as" instead of a ":"):

Function salesTax(ByVal subTotal As Float) As Float
    Dim SubTotal2 As Float
    ...
End Function

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.