3 ms·
Why make it easy to use something bad and hard to use something good? The concept of "this variable is mutable" makes more sense than "this variable is constant
by taktoa 9y ago
Why make it easy to use something bad and hard to use something good? The concept of "this variable is mutable" makes more sense than "this variable is constant" anyway; saying that a variable is mutable is like _adding features_ to it (i.e.: the ability to mutate it).
- gravypod 9y agoThe very name "variable" implies changing value. When you define something that doesn't change you are defining a "constant". I also don't think a mutable variable is bad or hard to use. Is this bad/hard to use? for (int i = 0; i < 10; i++) Or is this bad.... int line_number = 0; char *line; while (read_line_from(stdin, &line)) { // TODO handle line line_number += 1; } I don't think so. I think much of this buzz about how bad variables are comes from Rust merely existing and no practical examples. What inherently makes this for (mut int i = 0; i < 10; i++) better than our last example for loop? If you're actually against mutability where it doesn't need to be you can create tooling that can warn you of where you can add const. I think that's much better.
- catnaroek 9y ago> The very name "variable" implies changing value. Variables, both in ordinary mathematics and in the semantics of programming languages, are given meaning by substitution, not mutation. This is true even in the semantics of imperative languages[0][1]. [0] https://en.wikipedia.org/wiki/Hoare_logic https://en.wikipedia.org/wiki/Hoare_logic [1] https://en.wikipedia.org/wiki/Predicate_transformer_semantics https://en.wikipedia.org/wiki/Predicate_transformer_semantic...
- jononor 9y agoYour examples are too minimal. In most real-world cases there is a bunch of other variables also in scope of the loop. If all those variables are const you know that they are not changed in the loop (or elsewhere), simplifying understanding of the code and also reducing the chance of someone making the code more stateful later. With default-immutable this const is the default, making it harder to fuck up.