You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's not currently possible to define a function that takes a mut parameter that is a primitive (number, bool, etc) type. For example:
fn example() {
let mut x: u64 = 0
increment(x)
}
fn increment(mut x: u64) {
x += 1
}
This should modify the passed-in value in the calling scope. We've discussed adding the val-lang-inspired use of the & sigil to indicate mutation, which would make this more clear. Eg increment(&x)
How can it be fixed
This one's kinda complicated. The simple way would be to put all mutable integers in memory (or, better, those that are passed mutably into fns), so that we can pass a pointer into a function so that it can modify it, but that would be inefficient. In simple examples, inlining the function would solve the problem, but in complex cases it maybe be most efficient to compile it down to something like:
fn example() {
let mut x: u64 = 0
x = increment(x)
}
fn increment(x: u64) -> u64 {
return x + 1
}
so, mut primitive args would become return values, the args would be passed by value, and the value in the calling scope would be reassigned.
The text was updated successfully, but these errors were encountered:
What is wrong?
(#777 follow-up)
It's not currently possible to define a function that takes a
mut
parameter that is a primitive (number, bool, etc) type. For example:This should modify the passed-in value in the calling scope. We've discussed adding the val-lang-inspired use of the
&
sigil to indicate mutation, which would make this more clear. Egincrement(&x)
How can it be fixed
This one's kinda complicated. The simple way would be to put all mutable integers in memory (or, better, those that are passed mutably into fns), so that we can pass a pointer into a function so that it can modify it, but that would be inefficient. In simple examples, inlining the function would solve the problem, but in complex cases it maybe be most efficient to compile it down to something like:
so,
mut
primitive args would become return values, the args would be passed by value, and the value in the calling scope would be reassigned.The text was updated successfully, but these errors were encountered: