hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.23k stars 224 forks source link

[SUGGESTION] Allow `const` declarations to not require a wildcard `_` #1120

Closed DyXel closed 1 week ago

DyXel commented 2 weeks ago

This change would make the following compile:

main: () = {
    my_int1: const   = 1; // Errors today.
    my_int2: const _ = 2; // Above would be equivalent to this.
}

outputs:

test.cpp2...
test.cpp2(2,22): error: '*'/'const' type qualifiers must be followed by a type name or '_' wildcard (at '=')

Will your feature suggestion eliminate X% of security vulnerabilities of a given kind in current C++ code? No.

Will your feature suggestion automate or eliminate X% of current C++ guidance literature? No. Maybe for cpp2 itself? I am just surprised that just const doesn't work even though _ is already the default.

Describe alternatives you've considered. I added the _ as the compiler demanded, but guess what, I added it to the wrong side since I am so used to east-const šŸ˜… The reason I added the const to begin with was because I wanted to quickly check if the function was mutating my variable down below quickly, but got this error instead, this causes friction when iterating code.

*What about pointers ()?** I am not sure. There's a clang-tidy fix-it that automatically adds the pointer to auto for readability: readability-qualified-auto which seems to indicate some developers like this style?

Personally I don't think that my_pointer: * = something& would be more readable, it doesn't seem that way to me, it just seems redundant. I also don't know if allowing that would help meta/generation (like having trailing commas do).

gregmarr commented 2 weeks ago

Previous thread: #190

MaxSagebaum commented 1 week ago

Sounds good.

hsutter commented 1 week ago

Thanks! Done. I still like the explicit _ and will probably write that in my own code, but what pushed me over the edge is that this is a repeated request (thanks @gregmarr for reminding us about #190) and others seems to agree (thanks @MaxSagebaum)... and it doesn't appear to destabilize anything, it just requires actually removing the check.

bluetarpmedia commented 1 week ago

FYI see #1138