← Back to context

Comment by ekipan

3 days ago

Mutability is distinct from variability. In Javascript only because it's a pretty widely known syntax:

    const f = (x) => {
      const y = x + 1;
      return y + 1;
    }

y is an immutable variable. In f(3), y is 4, and in f(7), y is 8.

I've only glanced at this Zen-C thing but I presume it's the same story.

"immutable variable" is an oxymoron. Just because Javascript did it does not mean every new language has to do it the same way.

  • There are two distinct constructs that are referred to using the name variable in computer science:

    1) A ‘variable’ is an identifier which is bound to a fixed value by a definition;

    2) a ‘variable’ is a memory location, or a higher level approximation abstracting over memory locations, which is set to and may be changed to a value by an assignment;

    Both of the above are acceptable uses of the word. I am of the mindset that the non-independent existence of these two meanings in both languages and in discourse are a large and fundamental problem.

    I take the position that, inspired by mathematics, a variable should mean #1. Thereby making variables immutably bound to a fixed value. Meaning #2 should have some other name and require explicit use thereof.

    From the PLT and Maths background, a mutable variable is somewhat oxymoronic. So, I agree let’s not copy JavaScript, but let’s also not be dismissive of the usage of terminology that has long standing meanings (even when the varied meanings of a single term are quite opposite).

  • “Immutable” and “variable” generally refers to two different aspects of a variable’s lifetime, and they’re compatible with each other.

    In a function f(x), x is a variable because each time f is invoked, a different value can be provided for x. But that variable can be immutable within the body of the function. That’s what’s usually being referred to by “immutable variable”.

    This terminology is used across many different languages, and has nothing to do with Javascript specifically. For example, it’s common to describe pure functional languages by saying something like “all variables are immutable” (https://wiki.haskell.org/A_brief_introduction_to_Haskell).

  • I think you are confused by terminology here and not by behavior, "immutable variable" is a normal terminology in all languages and could be says to be distinct from constants.

    In Rust if you define with "let x = 1;" it's an immutable variable, and same with Kotlin "val x = 1;"

    • Lore and custom made "immutable variable" some kind of frequent idiomatic parlance, but it’s still an oxymoron in their general accepted isolated meanings.

      Neither "let" nor "val[ue]" implies constancy or vacillation in themselves without further context.

      4 replies →

  • Haskell, then.

        f x = let y = x + 1 in y + 1
    

    Same deal. In (f 3), y = 4, in (f 7), y = 8. y varies but cannot mutate. Should be a true enough Scottsman.