Timeline for Why do some languages have both immutable "variables" and constants?
Current License: CC BY-SA 4.0
19 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jun 26 at 3:28 | review | Suggested edits | |||
Jun 26 at 11:22 | |||||
Jun 20 at 7:50 | review | Suggested edits | |||
Jun 21 at 8:04 | |||||
Nov 26, 2023 at 11:28 | vote | accept | QAH | ||
Nov 22, 2023 at 5:30 | comment | added | Ethan B. | @PasserBy Huh, you're right. I didn't realize that macro expansion replaces identifiers rather than just any instance of the text (though it seems obvious in hindsight). Maybe should've taken that compilers class sometime... | |
Nov 22, 2023 at 3:40 | comment | added | Martin Kealey |
@AndrewHenle agreed; const only means it cannot be changed through this declaration, it does not mean nothing else can change it. So "immutable" is an overstatement.
|
|
Nov 21, 2023 at 10:52 | comment | added | MSalters | @MartinKealey: You're right for C, but C++ has constexpr as well. That is a true compile-time typed constant (has to be, because C++ templates are compile time, and those accept constexpr arguments). | |
Nov 21, 2023 at 4:26 | comment | added | Passer By | @EthanB. That won't compile, so no, it's not. | |
Nov 21, 2023 at 3:35 | comment | added | Ethan B. |
@PasserBy PI is just some text. If you use it like auto foo = PI , then foo is an int. If you use it like auto foo = PI.14 , then foo is a double.
|
|
Nov 20, 2023 at 15:22 | comment | added | Andrew Henle |
@MartinKealey C & C++ ... const denotes immutability IMO in C const is better characterized as "read-only, or else UB" per the wording of (draft) C11 6.7.3p3. There's even a const volatile example in the standard. volatile certainly implies anything but immutable.
|
|
Nov 20, 2023 at 15:05 | comment | added | Passer By |
@mousetail It's not. If you did that, PI is an int .
|
|
Nov 20, 2023 at 15:04 | comment | added | mousetail 'he-him' |
@PasserBy If you #define PI 3 it could be a int, short, float, long, unsigned char...
|
|
Nov 20, 2023 at 15:03 | comment | added | Passer By |
@mousetail With #define PI 3.14 , PI is a double . How is that losing type information or any different than, say, constexpr double pi = 3.14; ?
|
|
Nov 20, 2023 at 13:14 | comment | added | R.M. | I think C++'s "funny" laissez-faire approach to the distinction here is due to its "as if" rule. A C++ compiler is perfectly free to optimize out at compile time anything it doesn't need to have memory storage for (even non-marked "regular" variables!), and any compiler which doesn't do that for blatantly obvious cases is a bad compiler. So labeling in C++ is less about micromanaging compiler decisions, and more about giving the compiler enough information that it can do the best job of optimizing that it can.. | |
Nov 20, 2023 at 12:37 | comment | added | mousetail 'he-him' | @PasserBy Since they are just text copied into the code they lose type information | |
Nov 20, 2023 at 8:59 | comment | added | Passer By |
@apropos & is taking the address. Macros don't lose type information, they're just fancy copy-and-paste on your source code and a hugely problematic construct in many ways.
|
|
Nov 20, 2023 at 5:31 | comment | added | apropos |
@MartinKealey All of those approaches have drawbacks, I believe: #define loses type information, enum only works for integers (and loses type information), and I'm not quite sure what the & prefix denotes.
|
|
Nov 20, 2023 at 4:17 | comment | added | Martin Kealey |
C & C++ certainly do have compile-time constants, but they don't mark them by using const ; rather they mark them using enum or #define or &static_variable_name or &function_name . (Instead, const denotes immutability.)
|
|
Nov 19, 2023 at 17:37 | comment | added | Jörg W Mittag |
Scala has a notion of constant expressions for a similar construct. It is less like a language feature and more like language-mandated optimization: any field that is explicitly marked as private final , is not annotated with an explicit type, and is initialized with a literal expression, is guaranteed to always be inlined.
|
|
Nov 19, 2023 at 10:19 | history | answered | apropos | CC BY-SA 4.0 |