On Monday, February 24, 2025 11:19:48 AM MST Elias Batek (0xEAB) via Digitalmars-d-learn wrote: > For string constants you’ll usually want to mark them as `static > immutable`. Strings with this combination will usually be put > into read-only sections (ROM) of the resulting binaries. > > Unlike `static immutable` which specifies that data exists once > in a specific location, `enum` (“compile-time constant”) is more > similar to copy & paste where the compiler inserts the value into > each location where it is referred to.
When an enum is used, it's replaced with the value of the enum, so it's not strictly copy-paste. Any functions that were called to produce that value are called at compile time. The issue that you run into with most arrays is that you then get a different copy of that array (so a new allocation) every time that you use the enum (so it is copy-paste in that sense). However, with strings, that doesn't happen. You only end up with one allocation, and everywhere that you use the enum, it uses the same string. It's just that unlike an actual variable, it's an rvalue, so you can't take its address. I'm not aware of any reason to prefer a static immutable string over an enum unless you actually need to take its address for some reason. - Jonathan M Davis