> 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…
John Carmack on mutable variables
101–110 of 663 posts
Re: John Carmack on mutable variables
#102Earlier 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)
Re: John Carmack on mutable variables
#103Re: John Carmack on mutable variables
#104Earlier 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.
Re: John Carmack on mutable variables
#105Earlier 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.
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
#106Re: John Carmack on mutable variables
#107Earlier 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
Re: John Carmack on mutable variables
#108Re: John Carmack on mutable variables
#109Earlier 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
Re: John Carmack on mutable variables
#110Earlier 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.
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.