Declaring a single const: enum vs const vs immutable

2025-09-20 Thread Brother Bill via Digitalmars-d-learn
Is there any reason to pick one of these vs. another one, or are they all equivalent? If equivalent, it would seem that immutable appears to be the 'strongest', whereas enum has fewer keystrokes. Is there a D 'best practice' for this? ``` const int foo1 = 42; enum foo2 = 42;

Re: Declaring a single const: enum vs const vs immutable

2025-09-11 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Sep 10, 2025 at 05:15:16PM +, monkyyy via Digitalmars-d-learn wrote: [...] > I just do always enum; going all in on compile time abstractions Be careful, this may not always be what you want. For example: ```d enum data = [ 1, 2, 3 ]; void main() { auto buffer = data; //

Re: Declaring a single const: enum vs const vs immutable

2025-09-11 Thread IchorDev via Digitalmars-d-learn
On Wednesday, 10 September 2025 at 17:33:48 UTC, H. S. Teoh wrote: `enum` defines a compile-time constant. It occupies no space, and its value is "copied" into every expression in which it appears. [...] `const` and `immutable` are type qualifiers, and declaring a constant with them creates a

Re: Declaring a single const: enum vs const vs immutable

2025-09-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Sep 10, 2025 at 04:52:51PM +, Brother Bill via Digitalmars-d-learn wrote: > Is there any reason to pick one of these vs. another one, or are they > all equivalent? [...] They are most definitely not equivalent. `enum` defines a compile-time constant. It occupies no space, and its va

Re: Declaring a single const: enum vs const vs immutable

2025-09-10 Thread monkyyy via Digitalmars-d-learn
On Wednesday, 10 September 2025 at 16:52:51 UTC, Brother Bill wrote: Is there any reason to pick one of these vs. another one, or are they all equivalent? If equivalent, it would seem that immutable appears to be the 'strongest', whereas enum has fewer keystrokes. Is there a D 'best practice