We can circumvent mutability by assigning to fields of function returns #410
JSAbrahams
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description of Bug
Due to Python returning shallow copies from functions, we can reassign to fields of values even if the values themselves are immutable.
This behaviour was observed in #181, which contains an ignored test
assign_to_inner_non_mut3
.How to Reproduce
Expected behaviour
We expect a type error because while
my_field
is mutable, the class in which it is contained,C
, stored asmy_class
withinc
, is not.Additional context
It might be best to take a look at Rust's borrow system and take inspiration from there.
In Mamba, we could:
This is perhaps not the best alternative, as a lot of people coming from Python might run into unexpected behaviour if they often reassign to variables returned values of functions.
E.g. using getters directly:
my_object.get_my_field().inner_field <- 10
, will no longer have any effect as we reassign to a deep copy which is immediately discarded.deepcopy
.This would, however, result in a lot of unnecessary
deepcopy
invocations, which while useful in low-level languages, seems rather verbose for a more Python type language.If we do not access a field of self, we take the variable to be mutable.
This, I believe, most closely aligns with the philosophy of Mamba, since it reduces the verbosity, but still preserves the correctness of our type checker.
See Rust's borrow checker, which addresses an issue similar to ours (though their solution is more verbose due to Rust's low level nature).
Beta Was this translation helpful? Give feedback.
All reactions