Live data from Hacker News

John Carmack on mutable variables

twitter.com

221–230 of 663 posts

Re: John Carmack on mutable variables

#221

If designing your hypothetical ideal language, what are some intuitive/cute keywords you would choose for mutables/immutables? `let` is so 2020. `const` is too long. `static` makes me fall asleep at the keyboard. `con` sounds bad. How about `law`? law pi = 3.142 (heh typing on Mac autocompleted this) law c = 29979245 law law = "Dredd" or `set` or `once` or `make`?

[deleted]

Re: John Carmack on mutable variables

#223

Earlier quoted context omitted.

whats the difference between immutable and constant, which has been in use far longer? why are you calling it mutable?

Immutable and constant are the same. rendaw didn't use the word mutable. One reason someone might use the word "mutable" is that it's a succinct way of expressing an idea. Alternative ways of expressing the same idea are longer words (changeable, non-constant).

In languages like JavaScript, immutable and constant may be theoretically the same thing, but in practice "const" means a variable cannot be reassigned, while "immutable" means a value cannot be mutated in place.

They are very, very different semantically, because const is always local. Declaring something const has no effect on what happens with the value bound to a const variable anywhere else in the program. Whereas, immutability is a global property: An immutable array, for example, can be passed around and it will always be immutable.

JS has always hade 'freeze' as a kind of runtime immutability, and tooling like TS can provide for readonly types that provide immutability guarantees at compile time.

Re: John Carmack on mutable variables

#224

I like the idea of immutable-by-default, and in my own musings on this I've imagined a similar thing except that instead of a mutable keyword you'd have something more akin to Python's with blocks, something like: # Immutable by default x = 2 items = [1,2,3] with mutable(x, items): x = 3 items.append(4) # And now back to being immutable, these would error x = 5 items.append(6) I have put almost zero thought into the…

This is in essence a mutable borrow - by looking at Rust's borrow checker, one can see the complexities of the concept.

Clojure has transients—a similar idea I believe. Basically bounded mutation.

Re: John Carmack on mutable variables

#225

Earlier quoted context omitted.

In typescript and js you get immutable references, but the data is mutable. Definitely not the same thing

You have `as const`. Yes I know it's not enforced at runtime, but the type system does support it.

There’s Object.freeze for enforcing at runtime.

Re: John Carmack on mutable variables

#226
post #32
post #26

Earlier quoted context omitted.

> come with the baggage of foreign looking syntax Maybe they're right about the syntax too though? :)

Which one, Erlang, Lisp, or ML?

ML superfan here. I don’t mind lisp simplicity either. Erlang is too alien for me, but maybe once I was used to it.

Re: John Carmack on mutable variables

#227

Why loops specifically? Why not conditionals? A lot of code needs to assemble a result set based on if/then or switch statements. Maybe you could add those in each step of a chain of inline functions, but what if you need to skip some of that logic in certain cases? It's often much more readable to start off with a null result and put your (relatively functional) code inside if/then blocks to clearly show different l…

There’s no mutating happening here, for example: if cond: X = “yes” else: X = “no” X is only ever assigned once, it’s actually still purely functional. And in Rust or Lisp or other expression languages, you can do stuff like this: let X = if cond { “yes” } else { “no” }; That’s a lot nicer than a trinary operator!

IMO, both ternary operator form & Rust/Haskell/Zig syntax works pretty well. Both if expression syntax can be easily composed and read left-to-right, unlike Python's ` if else `.

Re: John Carmack on mutable variables

#228

Earlier quoted context omitted.

Much like PHP, you can actually get stuff done unlike a lot of other programming languages.

Oh? Which “lot of” other programming languages can’t you “actually get stuff done” in? Are you sure the problem lies with the programming language?

It’s an exaggeration perhaps but I get the sentiment. FP is elegant and beautiful and everything, but it can lead you to spend all day puzzling out the right abstractions for some data transformation that takes 5 minutes with a dumb for loop in Go.

Re: John Carmack on mutable variables

#230

After a 2 year Clojure stint I find it very hard to explain the clarity that comes with immutability for programmers used to trigger effects with a mutation. I think it may be one of those things you have to see in order to understand.

Made a similar experience with Scheme. I could tell people whatever I wanted, they wouldn't really realize how much cleaner and easier to test things could be, if we just used functions instead of mutating things around. And since I was the only one who had done projects in an FP language, and they only used non-FP languages like Java, Python, JavaScript and TypeScript before, they would continue to write things based on needless mutation. The issue was also, that using Python it can be hard to write functional style code in a readable way too. Even JS seems to lend itself better to that. What's more is, that one will probably find oneself hard pressed to find the functional data structures one might want to use and needs to work around recursion due to the limitations of those languages.

I think it's simply the difference between the curious mind, who explores stuff like Clojure off the job (or is very lucky to get a Clojure job) and the 9 to 5 worker, who doesn't know any better and has never experienced writing a FP codebase.

Post reply on HN