←back to thread

504 points azhenley | 1 comments | | HN request time: 0.208s | source
Show context
anymouse123456 ◴[] No.45771965[source]
I completely agree with the assertion and the benefits that ensue, but my attention is always snagged by the nomenclature.

I know there are alternate names available to us, but even in the context of this very conversation (and headline), the thing is being called a "variable."

What is a "variable" if not something that varies?

replies(14): >>45772017 #>>45772042 #>>45772062 #>>45772077 #>>45772262 #>>45772625 #>>45773368 #>>45773945 #>>45774039 #>>45775594 #>>45775669 #>>45775698 #>>45775911 #>>45776839 #
tialaramex ◴[] No.45772062[source]
In the cases we're interested in here the variable does vary, what it doesn't do is mutate.

Suppose I have a function which sums up all the prices of products in a cart, the total so far will frequently mutate, that's fine. In Rust we need to mark this variable "mut" because it will be mutated as each product's price is added.

After calculating this total, we also add $10 shipping charge. That's a constant, we're (for this piece of code) always saying $10. That's not a variable it's a constant. In Rust we'd use `const` for this but in C you need to use the C pre-processor language instead to make constants, which is kinda wild.

However for each time this function runs we do also need to get the customer ID. The customer ID will vary each time this function runs, as different customers check out their purchases, but it does not mutate during function execution like that total earlier, in Rust these variables don't need an annotation, this is the default. In C you'd ideally want to label these "const" which is the confusing name C gives to immutable variables.

replies(3): >>45772342 #>>45773930 #>>45776499 #
tredre3 ◴[] No.45776499[source]
> In Rust we'd use `const` for this but in C you need to use the C pre-processor language instead to make constants, which is kinda wild.

I get that you're not very familiar with C? Because in C we'd use const as well.

    const int x = 2;
    x = 3; // error: assignment of read-only variable 'x'
replies(2): >>45777534 #>>45777589 #
tialaramex ◴[] No.45777589[source]
That's not a constant, that's an immutable variable which is why your diagnostic said it was read-only.

   const int x = 2;
   int *p = &x;
   *p = 3; // Now x is 3
And since I paid for the place where I'm writing this with cash earned writing C a decade or so ago, I think we can rule out "unfamiliar with C" as a symptom.
replies(1): >>45787212 #
1. tredre3 ◴[] No.45787212[source]
Now x is 3 but you also get a compiler warning telling you not to do that.

In my opinion it's a bit disingenuous to argue that it isn't a const just because you can ignore the compiler and shoot yourself in the foot. If you listen to the compiler, it is reflected in the assembly that it is a constant value the same as #define x 2.

Is Rust better at enforcing guarantees? Of course. Is `const` in C `const` if you don't ignore compiler warnings and errors? Also of course.

> And since I paid for the place where I'm writing this with cash earned writing C a decade or so ago

Ditto!