Declaring a single const: enum vs const vs immutable
H. S. Teoh
hsteoh at qfbox.info
Wed Sep 10 17:41:59 UTC 2025
On Wed, Sep 10, 2025 at 05:15:16PM +0000, 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; // GC allocation
...
auto tmpBuf = data; // another GC allocation
...
assert(buffer is tmpBuf); // fails
}
```
Whereas:
```d
static immutable data = [ 1, 2, 3 ];
void main() {
auto buffer = data; // no GC allocation
...
auto tmpBuf = data; // no GC allocation
...
assert(buffer is tmpBuf); // true
}
```
T
--
Never wrestle a pig. You both get covered in mud, and the pig likes it.
More information about the Digitalmars-d-learn
mailing list