3 ms·
> The greatest sinners are methods that both mutate and return their result. Go's `append` builtin is guilty of exactly that sin. And worse, it does not always
by masklinn 8y ago
> The greatest sinners are methods that both mutate and return their result.
Go's `append` builtin is guilty of exactly that sin. And worse, it does not always mutate its parameter in-place, so things get hellish if you start aliasing/shallow-copying slices because depending on the size of the underlying array, appends could overwrite the other slice's data or make the two slices diverge entirely.
- Sir_Cmpwn 8y agoappend has a good reason for that, it can potentially reallocate the slice and return a new pointer. This is an expression of the underlying truth of the implementation that they chose not to abstract over, a decision I personally appreciate.
- TheCoelacanth 8y agoThat's just a language that is insufficiently powerful to express the underlying truth. In C++ or Rust, the same construct could be expressed without loss of efficiency and without the leaky abstraction.
- Sir_Cmpwn 8y agoCan you elaborate?
- TheCycoONE 8y agoNot the original poster, but in C++ you would be using a container, likely a vector so the internal data could move, but because the container is updated the caller can be unaware. The abstraction leaks a bit. The result of a data() or c_str() call cannot be trusted after the vector is manipulated. When passing a vector by reference to another function you effectively get a double pointer (abstracted behind a reference and a container) so the inner one can be moved. I'm not familiar with go, but if it is like C in this respect append would have been clearer with a double pointer and no return value.