Mutability design (`mut`)
bvssvni opened this issue · comments
Sven Nilsen commented
Dyon uses mut
to declare mutability. This appears in front of a function argument declaration or when calling a function that mutates the argument.
foo(mut a)
mutates the argumenta
foo(mut a)
has function namefoo(mut)
foo(mut a, b)
has function namefoo(mut,_)
foo(a, b)
has function namefoo
Local variables are mutable.
Mutability is not part of a type, but added to the function name inside parentheses, e.g.(mut,_)
. When adding an external function, the mutability information must be added as part of the function name.
Mutability propagates for arguments:
fn foo(mut a) { ... }
// `bar` requires `mut a` because it calls `foo(mut a)`.
fn bar(mut a) { foo(mut a) }
Multiple functions can use the same name if they have different mutability information. This reduces the need for "get", "set" and "mut" in function name:
fn title(window: 'return) -> { ... }
fn title(mut window, val) { ... }
t := title(window)
title(mut window, "hello world!")
This is designed for:
- Explicitly declare when a function mutates a variable
- Improve readability and maintenance
- Allow a function name to be reused for different mutability patterns