5 ms·
> Immutable data structures (but not variables) Why not not immutable variables? About 90% of my variables I only assign once, even when writing ordinary impe
by qwerty456127 4y ago
> Immutable data structures (but not variables)
Why not not immutable variables?
About 90% of my variables I only assign once, even when writing ordinary imperative (not functional) code. Wehever a variable is re-assigned that usually is a mistake or or something unexpected going on.
I find it maddening most of the languages don't have single-assign values like Scala's val (const doesn't count because you can't initialize it with a runtime-computed value).
- unsafecast 4y ago> const doesn't count because you can't initialize it with a runtime-computed value In what language? C's const is an unmodifiable runtime value for example, as is Zig's. Go is one language that I can think of where const is compile-time though.
- winrid 4y agoAlso Nim.
- auxym 4y agoIn Nim, let is an immutable runtime value, but const is an immutable compile time value, sort of like constexpr in recent c++.
- tialaramex 4y agoC and C++ "const" is weird because it's not constant, it's just a weird name for immutable - K&R C doesn't have const, it's an invention of C++ and thus modern C. Bjarne's book calls these immutable variables "constants" which is completely crazy, but I guess indicates that the name is on purpose. This leads to the usual C++ insanity shortly afterwards in the book... > An object that is a constant when accessed through one pointer may be variable when accessed in other ways. Yeah, no. If the object were actually constant you can't do that, your language has betrayed the poor programmer. You can't "access" constants, you can use them to initialize variables, parameters, and so on, but they can't be "accessed" and that's where you've gone wrong here. Rust's Quiz has a example showing what happens with actual constants, and it's not that
- unwind 4y agoIt's fun how you refer to anything after K&R as "modern C"; since ANSI C was first specified in 1989, this means that any version since then is modern. Great! I do agree that "immutable" would have been a better name, but I guess that angle hadn't been invented yet. :)
- tialaramex 4y agoSomething, something, get off my lawn? Actually I wasn't getting paid to write C back in 1989, but I definitely did write C in the era when ANSI C wouldn't have been generally accepted. Immutable variables are a good idea (indeed I agree with Rust's choice to make variables immutable by default), but, they're not constants, and so this is an unnecessary confusion.
- pierrebai 4y agoYou are merely very hung up on nomenclature. They had to choose a keyword, they chose const. It's very common: people have mental models and pre-conception about what word means. From that point, when in a specific context where their innate expectation are not met, they can have one of two reactions: adapt to the specific meaning in that specific context (mental flexibility) or reject and rant that things are wrong (mental rigidity). Different people will have different breaking points about when things have been stretched too far, meaning they are not willing to compromise on. Working with people who have not the same level of flexibility or rigidity or differ in different contexts can be a day-to-day pain.
- tialaramex 4y agoNomenclature is certainly not great, but the trouble as so often goes deeper. Bjarne has these "constants" and so he can't see any reason he'd need actual constants which are, you know, constant. If he recognised that he's got immutable variables, and not constants, then the need for actual constants is a little more obvious.
- metadat 4y ago
- riffraff 4y agoI would guess because when you're doing a hacky thing, it's convenient to go back and make a change like "x = something with x" rather than updating all occurrences. Source: I do that all the time when trying to solve AoC problems :)
- widforss 4y agoBecause you cannot have loops in an immutable universe? Sure, you can map functions, but sometimes loops are fun too.
- tialaramex 4y agoIt's actually better if you have immutability here, languages which try to instead have a single variable for the values, mutating that variable each time around the loop, get into some trouble. Compare Rust's for loop: for k in 0..10 { // k is an immutable variable, brought into existence for one iteration // next time around the loop that's a different immutable k } Versus the C for loop: for (k = 0; k < 10; ++k) { // k is a long-lived mutable variable, we can change k inside the loop and it'll remember! } We can tell Rust we want a mutable variable, if we want, but it's not the same: for mut k in 0..10 { // k is a mutable variable... // but still brought into existence for each iteration // changing will work in this scope... but it won't last }
- widforss 4y agoCool, I didn't know that Rust did that (or have forgotten)! But it sure looks like a map where we throw away the result. What I mean with that is that the loop construct must be served the iterations from an iterator, which may or may not use immutable data. I don't think you can make an immutable while loop, for example? EDIT: Also, you better have a side-effect in that construct, otherwise it has no effect, right?
- qwerty456127 4y agoI mean both ways should be available. It generally is reasonable to support mutable variables but immutable values should be supported as well. E.g. Scala has 2 separate keywords: var and val (the difference is you can't assign a val again once it is initialized). I want the same in C#, Python and other languages.
- cerved 4y agoalso JS let/const
- betaveros 4y agoI like this idea. I think I've considered adding immutable variables, but haven't prioritized them because other things I wanted to work on have a better expressiveness or bug-catching ability to effort ratio. Plus I haven't thought of a good syntax for them. But I might eventually get around to it.