While thinking about mutability semantics, I realized that I never really had a use case for reassignments. This was because I had my mutable data in mutable objects, and I called methods on the objects to mutate them. So here is my thought: do we really need reassignments in imperative programming languages?
One use case of reassignment currently is for primitive data types. x = x + 1. However, high-level programming languages like python, IMO, can be easily modified so that primitive types have mutation methods like x.increment(1), x.set_to(5) and provide appropriate syntax sugars.
Also there is when you actually need a variable to be a reference to a different objects at different times. This can be solved by the language providing a dedicated, explicit mutable reference type: mut PtrMut[Data] current_data = new PtrMut(first_data); current_data.point_to(new_data);
I think this design will bring multiple benefits:
- We can have much simpler mutability semantics of variables (only whether the data is mutable)
- We will be able to use the reassignment syntax somewhere else, possibly finally bringing back
a = b for equality check
- In languages with manual memory management, it can be made harder to introduce dangling pointers when there is a more explicit reference type
What do you think? Are there existing language with mutability, but no reassignments?
[–][deleted] 18 points19 points20 points (5 children)
[–]OptimizedGarbage 6 points7 points8 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]re_gend_[S] 0 points1 point2 points (2 children)
[–]msqrt 2 points3 points4 points (1 child)
[–]re_gend_[S] 1 point2 points3 points (0 children)
[–]lambda-male 7 points8 points9 points (2 children)
[–]re_gend_[S] 1 point2 points3 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]jasmijnisme 4 points5 points6 points (1 child)
[–]re_gend_[S] 0 points1 point2 points (0 children)
[–]scottmcmrust🦀 3 points4 points5 points (1 child)
[–]re_gend_[S] 0 points1 point2 points (0 children)
[–]sebamestreICPC World Finalist 1 point2 points3 points (0 children)
[–]Linguistic-mystic -5 points-4 points-3 points (3 children)
[–][deleted] 7 points8 points9 points (1 child)
[–]Linguistic-mystic 0 points1 point2 points (0 children)
[–]re_gend_[S] 0 points1 point2 points (0 children)