I just had a thought! It might make languages like Rust more ergonomic if movement could also update the reference to the new location so that moving a local variable into a container could also update the variable to reference the target also.
The "broken" example can be trivially fixed:
fn main() {
let id = Id(5);
let mut v = vec![id];
let id = v[0].0; // just use the new name!
println!("{}", id );
}
But what if the language supported "move and in-place update the reference"?
Something like:
let mut v = vec![@id]; // Some new symbol
Where '@' (or whatever) is a new operator that says: move the object and update the reference `id` to point to the moved value. This could only be used for parameters marked with some sort of attribute indicating that this is possible.
On its own, that doesn't sound like the worst idea. But at least in Rust, one problem is that further modifying the container will immediately invalidate the new reference, as will reading from the container if you get a mutable reference. References into containers are temperamental in general, unless you can handle them in smaller scopes that are unaware of the larger container.
But why add a language feature (and a new symbol, even!) when by definition you can always fix the issue by shadowing the original variable and pointing it at the new location? At most, this calls for a change in compiler diagnostics to add a hint in cases that are trivially fixable.
There are scenarios where retrieving the just-inserted value without first cloning it isn't possible. E.g.: when inserting a value into a non-empty hashset and then needing it again immediately.
But yeah... that's a bit of a contrived example and can be solved by a simple change to the insert function without specialised support from the language.
(Emphasis mine.)
I just had a thought! It might make languages like Rust more ergonomic if movement could also update the reference to the new location so that moving a local variable into a container could also update the variable to reference the target also.
The "broken" example can be trivially fixed:
But what if the language supported "move and in-place update the reference"?Something like:
Where '@' (or whatever) is a new operator that says: move the object and update the reference `id` to point to the moved value. This could only be used for parameters marked with some sort of attribute indicating that this is possible.