Live data from Hacker News

John Carmack on mutable variables

twitter.com

101–110 of 663 posts

Re: John Carmack on mutable variables

#101
post #7
post #2

> Making almost every variable const at initialization is good practice. I wish it was the default, and mutable was a keyword. It's funny how functional programming is slowly becoming the best practice for modern code (pure functions, no side-effects), yet functional programming languages are still considered fringe tech for some reason. If you want a language where const is the default and mutable is a keyword, try…

> If you want a language where const is the default and mutable is a keyword, try F# for starters. I switched and never looked back. Rust is also like this (let x = 5; / let mut x = 5;). Or you can also use javascript, typescript and zig like this. Just default to declaring variables with const instead of let / var. Or swift, which has let (const) vs var (mutable). FP got there first, but you don't need to use F# to…

In Java you can use final[1]. And yes, if final points to an ArrayList you can change it, but you can also use final together with immutable data structures[2].

[1]: https://www.baeldung.com/java-final

[2]: https://www.baeldung.com/java-immutable-list

Re: John Carmack on mutable variables

#102
post #7

Earlier quoted context omitted.

> If you want a language where const is the default and mutable is a keyword, try F# for starters. I switched and never looked back. Rust is also like this (let x = 5; / let mut x = 5;). Or you can also use javascript, typescript and zig like this. Just default to declaring variables with const instead of let / var. Or swift, which has let (const) vs var (mutable). FP got there first, but you don't need to use F# to…

Are there languages that automatically extend this to things like data structure members? One of the things I like about the C++ const keyword is that if you declare an instance of a struct/class as const it extends that to its members. If the instance isn’t const, you can still mutate them (as long as they aren’t declared const within the structure itself)

If I understand what you’re asking correctly, rust is also like this. If you have a non-mut value of (or non-mut reference to) an object, you only get non-mut access to its members.

Re: John Carmack on mutable variables

#104
post #100

Earlier quoted context omitted.

Are there languages that automatically extend this to things like data structure members? One of the things I like about the C++ const keyword is that if you declare an instance of a struct/class as const it extends that to its members. If the instance isn’t const, you can still mutate them (as long as they aren’t declared const within the structure itself)

Rust works this way, yes. There are escape hatches though, which allow interior mutability.

Yeah, here's an example: https://play.rust-lang.org/?version=stable&mode=debug&editio...

Re: John Carmack on mutable variables

#105
post #85

Earlier quoted context omitted.

My problem with functional languages is there never seems to be any easy way to start using them. Haskell is a great example here. Last time I tried to learn it, going on the IRC channel or looking up books it was nothing but a flood of "Oh, don't do that, that's not a good way to do things." It seemed like nothing was really settled and everything was just a little broken. I mean, Haskell has like what, 2, 3, 4? Maj…

> Lisp is also a huge train wreck that way. [...] There's like 20+ different lisp like languages. Lisp is not a language, but a descriptor for a family of languages. Most Lisps are not functional in the modern sense either. Similarly, there are functional C-like languages, but not all C-likes are functional, and "learn c-likes" is vague the same way "learn lisp" is.

You’re right and this is also a bit of a pet peeve of mine. “Lisp” hasn’t described a single language for more than forty years, but people still talk about it as if it were one.

Emacs lisp and Clojure are about as similar as Java and Rust. The shared heritage is apparent but the experience of actually using them is wildly different.

Btw, if someone wants to try a lisp that is quite functional in the modern sense (though not pure), Clojure is a great choice.

Re: John Carmack on mutable variables

#106

Rediscovering one of the many great decisions that Erlang made

To be fair, Carmack has advocated for immutability and other good practices at least since the 2000s.

Maybe he read SICP then. But he still didn't write a compiler since

Re: John Carmack on mutable variables

#107
post #7

Earlier quoted context omitted.

> If you want a language where const is the default and mutable is a keyword, try F# for starters. I switched and never looked back. Rust is also like this (let x = 5; / let mut x = 5;). Or you can also use javascript, typescript and zig like this. Just default to declaring variables with const instead of let / var. Or swift, which has let (const) vs var (mutable). FP got there first, but you don't need to use F# to…

In Java you can use final[1]. And yes, if final points to an ArrayList you can change it, but you can also use final together with immutable data structures[2]. [1]: https://www.baeldung.com/java-final [2]: https://www.baeldung.com/java-immutable-list

Did you know that "final" does not actually mean final in Java (as in: the variable can be constant folded)? Reasons include reflection and serialization (the feature that nowadays nobody uses, but due to backwards compatibility the Java language developers always have to worry about?). There was an excellent talk about this recently, I think triggered by a new JEP "stable values": https://youtu.be/FLXaRJaWlu4

Re: John Carmack on mutable variables

#109
post #84
post #10

Earlier quoted context omitted.

Functional programming languages (almost always?) come with the baggage of foreign looking syntax. Additionally, imperative is easier in some situations, so having that escape hatch is great. I think that's why we're seeing a lot of what you're describing. E.g. with Rust you end up writing mostly functional code with a bit of imperative mixed in. Additional, most software is not pure (human input, disk, network, etc)…

Exactly this! I’d love a modern C++ like syntax with the expressiveness of python and a mostly functional approach. C# is not that far I suppose from what I want

Everybody's mileage will vary, but I find contemporary C# to be an impressively well rounded language and ecosystem. It's wonderfully boring, in the most positive sense of the word.

Re: John Carmack on mutable variables

#110

Earlier quoted context omitted.

Your IDE probably supports this as an explicit action. JetBrains has a feature that can find all reads and writes to a variable

It also has the ability to style mutated variables differently.

Yes, depending on your highlighting scheme. Not every highlighting scheme shows this by default, unfortunately.

To me, this seems initially like some very minor thing, but I find this very helpful working with non-trivial code. For larger methods you can directly discern whether a not-as-immutable-declared variable behaves immutable nonetheless.

Post reply on HN