-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
fix language inconsistency for mutating methods #9013
Comments
Is it already decided that if by accident |
I'm still new to the community.. why is Why not allow fn (mut item Item) sort!() { } // proper usage
fn (item Item) sort!() // warning (no mut)
fn (mut item Item) sort() // warning (no !) |
I don't think it's off the table. But I was just asking about potential conflicts depending on other decisions (I'm not following V's development due to lack of time, but I know there are other syntax-related ongoing discussions). |
That sounds useful. Plus if I expect a method with mutable receiver that actually returns a copy of the receiver without mutation, then I would get an error: fn (r R) meth() R
...
mut rec = R{}
rec!meth() // error, `meth` does not mutate receiver` Currently that would be a silent bug.
That seems like unnecessary complexity.
We do have |
What does this use of |
It's not documented. It means a fixed-size array literal. |
What about
? |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Currently mutation is explicit for function calls but not method calls:
sort(mut mutable_item)
// here the current syntax makes mutation explicit
mutable_item.sort()
// here the current syntax allows silent mutation
// for the same functionality
Since the extremely minimal syntax of appending an exclamation mark, as in
mutable_item.sort!()
, has apparently already been rejected by the community....Here I'm suggesting:
mutable_item!sort()
// the new syntax would use an exclamation mark
// in place of the dot operator for mutating method calls
I'm not sure how we can cut down the typing much more than that.
Replacing the dot with ! means one less keypress than
sort!()
and it has an added advantage: with autocompletion it can trigger a filtering of suggestions to include only mutating methods. Finally, this way methods don't need a special name.That would mean there may actually be no net extra typing.
Another option could be capitalizing:
mutable_item.Sort()
// Same benefits as ! but actually also enhances the readability, I think
If this change makes it in, I'm imagining that at first the compiler would just give a warning when the new syntax is expected.
The text was updated successfully, but these errors were encountered: