-
Notifications
You must be signed in to change notification settings - Fork 258
Const by default #815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is already the case for function arguments, which default to pass-by |
Statements can also have parameters (https://github.com/hsutter/cppfront/wiki/Design-note%3A-Defaults-are-one-way-to-say-the-same-thing#from-named-functions-to-lambdas-to-parameterized-blocksstatements-to-ordinary-blocksstatements, #386 (comment)).
|
Declaring a runtime constant is quite noisy: v := expr;
c: const _ = expr; That's enough typing and noise to avoid marking constants. So the status quo encourages bad practice: From https://github.com/hsutter/cppfront/wiki/Design-note:-const-objects-by-default:
I believe this statement is false in modern code. The author of the Vale language wrote:
https://verdagon.dev/blog/on-removing-let-let-mut#why-we-like-it |
Instead of switching to the default and requiring mut, or similar, perhaps :== could imply const?
On 24 November 2023 14:44:36 Nick Treleaven ***@***.***> wrote:
Declaring a runtime constant is quite noisy:
v := expr;
c: const _ = expr;
That's enough typing and noise to avoid marking constants. So the status quo encourages bad practice:
https://www.reddit.com/r/cpp2/comments/16ftsw7/suggestion_local_objects_const_by_default/
From https://github.com/hsutter/cppfront/wiki/Design-note:-const-objects-by-default:
the majority of local variables are, well, variables
I believe this statement is false in modern code. The author of the Vale language wrote:
To our great surprise, we've found that our codebases have a lot more declarations than assignments
We sampled three Vale projects. One had 111 declarations, and only 35 assignments. That's only 21% assignments! The other two were even lower, at 20% and 6%.
This isn't just Vale either. A randomly chosen Rust library, Rocket<https://github.com/SergioBenitez/Rocket>, had about 8%.
https://verdagon.dev/blog/on-removing-let-let-mut#why-we-like-it
—
Reply to this email directly, view it on GitHub<#815 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AALUZQNPPJRWWZMMX6JJL53YGCXFBAVCNFSM6AAAAAA7IB4US2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMRVG43TCMBUGA>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Thanks, data is great and I appreciate it. Followup questions please:
Do you know of data on larger projects, ideally C/C++/C#/Java code, and counting the other non- |
I don't have the data but here's an article by creator of Odin language which talks about various kinds of syntax for declarations. cpp2 seems to fall in the group of "name-focused" declarations and I can't help but notice that the two languages (Odin and Jai) which have name-focused declarations have same syntax for function, type and constant declarations. That is expected as functions and types are kind of "constant" bindings, you can't assign a new function to same name (not true for anonymous function but well, they are anonymous). Even Zig declares struct like the following const A = struct {
x: f32,
y: f32,
}; cpp2 has an inconsistency in this case. |
|
That's not very obvious (I didn't even think about it like that before). I also don't know how that extends to functions and the |
Named functions are also constant. |
Named functions are also constant, |
They can be constant. |
I tried an experiment where every local variable is emitted as Of 98 test cases, 41 succeeded and 57 failed. It seemed that the 41 were mostly the smaller/simpler ones that were exercising the compiler (e.g., testing grammar or lowering) rather than trying to do a sample computation. In some cases, making a local variable Some types' natural usage is non-
Other patterns I noticed:
I stopped partway through... the results weren't encouraging enough to continue the experiment at this time, but I'm open to taking it up again in the future. For now, my results seem to confirm the hypothesis that 'local variables tend to be, well, variables.' |
These are inherently static (compile-time) and ODR-necessary in a statically typed language. Unlike objects, which are inherently run-time dynamic values (and sometimes dynamic types, see previous).
(I have to poke a gentle smile at "variables" in the quote here -- I know you mean "objects," but the phrasing "variables be constant" is a little self-answering so I'll point it out just a little bit. 😁 ) Many Cpp2 objects are I do think it's interesting that the discussion tends to focus on function scope objects, but not type scope objects. So I have a suggestion... instead of focusing only on function local objects, please also consider type scope objects: Why (or why not) should type scope objects be |
You've very much convinced me. cpp is a language that doesn't lend itself to have Also, const member data doesn't make much sense, they come with problems like making the class's move constructor slower, no std::swap etc. And the better way is to have private members with no public API to change it. But, can cpp2 still have some terser way to declare const variables? |
These are the related guidelines that are still relevant in Cpp2:
Type scope objects shouldn't be When commit Now I'm generalizing It seems like how many non- |
I believe that knowing which variable is subject to change easily reduce the mental overhead of code debug/reviewing/maintenance.
The issue with C++ is that you can declare a variable that should not change by forgetting the 'const' keyword.
C++ compilers throw an error when modifying a const variable but they can't tell you when you are modifying a variable that you shouldn't because of algorithm logic.
Adding 'const' to any variable and function argument is a practice that my team and I have been practicing over 3 years. The obvious issue with this practice is the increased mental overhead of writing code, as you have to remember it when writing a variable.
So, what do you think about initializing variable and function arguments with const by default ?
We can use a 'mutable' or 'mut' keyword to tell that the variable is subject to change when it is really required.
The text was updated successfully, but these errors were encountered: