Re:Copying data is wasteful, mutating data is dangerous
by masklinn on Thursday January 09, 2020 @01:47PM
(#22004154)
> As far as I know, only Rust, C++, and C give support at the type-checking level to denote which functions will modify the interior of their arguments.
1. C and C++ allow casting away constness, which may or may not be UB depending how the parameter is defined
2. Swift also provides this ability to an extent: struct parameters have to be flagged `inout` to be mutable
I'm sure there are others.
Re:Copying data is wasteful, mutating data is dangerous
by nwallin on Thursday January 09, 2020 @03:49PM
(#22005534)
Rust can throw away constness in unsafe blocks, which carries the same caveat emptor that const_cast carries.
Re:Copying data is wasteful, mutating data is dangerous
by steveklabnik on Thursday January 09, 2020 @04:42PM
(#22006190)
Only in certain circumstances, though.
Re:Copying data is wasteful, mutating data is dangerous
by gdxhyrd on Thursday January 09, 2020 @02:07PM
(#22004350)
Casting away constness in C++ is something it is virtually never done, and has to be done very explicitly.
Re:Copying data is wasteful, mutating data is dangerous
by masklinn on Thursday January 09, 2020 @03:02PM
(#22005025)
> https://github.com/search?q=const_cast&type=Code
We must have very different notions of "virtually never": https://github.com/search?q=const_cast&type=Code
> has to be done very explicitly.
That doesn't make it visible to the caller.
Theoretically, you can make every API mutate data. If the user didn't want to mutate data, they can explicitly make a copy first. Whereas if there only exists an immutable API to return new objects, then it's hard or impossible to get the benefits of mutating in place.
As far as I know, only Rust, C++, and C give support at the type-checking level to denote which functions will modify the interior of their arguments. This way, you can distinguish behaviors easily - for example in Rust:
The article gave this subtly flawed example in Python: In Rust, it would be a type error because visualize() takes a reference but normalize_in_place() takes a mutable reference: