Allow `mut` function parameters of primitive type
- Dominant language
- Rust
- Stars
- 1.7k
- Forks
- 218
- Avg merge
- 1d 4h
- Merged PRs (30d)
- 8
Description
### 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:
```
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.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reviewing the referenced #777 follow-up and the alternatives described here for mutable primitive parameters. Done means deciding the mutation syntax and semantics, then implementing support without unnecessarily placing all mutable integers in memory.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100