4 ms·
> 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, a
by 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 agoWhat do you consider #define to be doing? Simple usage arguably provides a way to have actual constants.
- unsafecast 4y agoBut not runtime constants. I do agree with GP in that it would've been nice to have actual constant values, but it is what it is. The last thing C and C++ need is more features. C because we don't want to ruin it, and C++ because we want to slow the death.
- int_19h 4y agoIt's a weird name for "readonly", not "immutable", as evidenced by this example: int x = 0; int const *px = &x; ++x; // *px changed There's no way to indicate true immutability in C++, unfortunately. The language does decree that any object (which in C++ parlance includes ints etc) with a const type cannot be mutated without triggering UB; but there's no way to declare a pointer or reference that can only point to such objects.