-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Explicit use of the 'var' keyword for pointers and arrays to mutable data #5056
Comments
per my comments on irc, i don't have any particular interest in throwing more keywords all over my code, but i'm not so opposed to this if it's a reversal of default mutability to default immutability |
In my opinion, the best solution would be immutability be default and Pros
Cons
Edit: After thinking about it, with #4820 being accepted, I think |
@nycex It is consistent
|
The Pros and Cons I listed are for Forcing the user to add |
@theInkSquid I think @nycex was referring to To be honest, I would also prefer default immutability for pointers and arrays, as the risk for non const-correct code increases with pointers to pointers and so on, and currently securing them harms readability. For example, But should the Zig community refuse to go back to default immutability for whatever reason, forcing the user to choose between Edit: fixed description for *var *var Foo. |
consider this example to avoid a copy; we can add
|
@mikdusan I'm not sure why the |
you're right it's not needed. edited. |
right, however with our current syntax we cannot do what is shown. And if we examine the possible choices available, as long as params stay immutable-by-default, we need a contrary modifier. This shows it would also be nicely consistent with this PR. |
@mikdusan That's for sure, but I encourage you to write a separate proposal for this instead. Otherwise, this proposal might become too big or cumbersome to be accepted and nor mine and your proposals would be finally accepted. |
This was one of the things I considered early on, and landed confidently on status quo.
Not planned. Status quo of |
As of zig 0.6.0, pointers can be defined as
*type
or*const type
, whereas array (when used as function parameters) can be defined as[]type
or[]const type
. This means they are mutable by default, similarly to C and C++.However, experience has proven default mutability might cause bugs and/or unexpected behaviour, especially when pointers (or pointers to pointers, etc.) are involved. We must take into we programmers are lazy and often prefer omitting a few keystrokes (especially among beginners) over adding zero-cost safety. Therefore, I want to make a proposal that aims to be consistent with the rest of the Zig language:
*var type
for pointers to mutable data and[]var type
for arrays to mutable data.*const type
/[]const type
.IMHO, this style is consistent with the existing way of defining variables in Zig via the
var
/const
keywords. Whereas default immutability could be instead applied (as in Rust), forcing the user to choose eitherconst
orvar
still makes the intention clear for the reader and fits better with the language.P.S.: while discussing this proposal on IRC, some users preferred using
mut
overvar
for this specific situation, as it would cause confusion whenvar
is used as a type. However, according to #4820var
would be renamed toanytype
, avoiding such confusion.The text was updated successfully, but these errors were encountered: