(yes yes, I know, that would break syntax... but please come up with something to discourage mutability)
(yes yes, I know, that would break syntax... but please come up with something to discourage mutability)
Yeah I know there's ways around it, but then the author known what they told the other party to expect.
Even the JS standard library struggles with this. You just have to remember that .sort() modifies the array in place (and returns it), but .slice() does a shallow clone of the array. (Not a deep clone - that would be different again!)
I don't think this represents struggling. There needs to be some way to sort in place. Sometimes you need to sort a big array and don't want to allocate.
And there should be some way to clone the array without mutating. That's slice. So you how do you sort a clone? .slice().sort()
I think by far the biggest problem with ES .sort() is that number arrays don't sort numerically by default.
Your parent isn't saying there shouldn't be mutation, just that the mutation should be obvious.
In Rust, the type signature for the in-place sort is
pub fn sort(&mut self)
where
T: Ord,
That `&mut self` lets you know that it's going to mutate. foo(x); // Moves or copies
foo(&x); // immutable reference
foo(&mut x); // mutable reference
I don’t need to look up the signature of foo to understand what happens to my variable. It’s obvious at a glance.