digitalmars.D.learn - Newbie style question about string constants
- Ian (8/8) Feb 24 Hello,
- monkyyy (7/11) Feb 24 enum vs value is a tradeoff of when a decision is made; not some
- Elias Batek (0xEAB) (8/16) Feb 24 For string constants you’ll usually want to mark them as `static
- Jonathan M Davis (12/19) Feb 24 When an enum is used, it's replaced with the value of the enum, so it's ...
- Jonathan M Davis (27/33) Feb 24 For strings, the way that you normally do constants is with enum, e.g
Hello, What's the recommended D way to declare a string constant? Say, for example, for a path used inside a class constructor? I've seen const string, immutable, enum etc... Is this the place for these kinds of questions? Is there a D stack overflow? Cheers, Ian
Feb 24
On Monday, 24 February 2025 at 16:07:07 UTC, Ian wrote:Hello, What's the recommended D way to declare a string constant? Say, for example, for a path used inside a class constructor? I've seen const string, immutable, enum etc...enum vs value is a tradeoff of when a decision is made; not some oo thoery If something is known compile time and you cant even consider how it would change, make it an enum and your have less runtime errors, dodge some conversions, allow more free optimization ~~const and immutable can be deleted from the language~~
Feb 24
On Monday, 24 February 2025 at 16:07:07 UTC, Ian wrote:Hello, What's the recommended D way to declare a string constant? Say, for example, for a path used inside a class constructor? I've seen const string, immutable, enum etc... Is this the place for these kinds of questions? Is there a D stack overflow? Cheers, IanFor 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.
Feb 24
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
Feb 24
On Monday, February 24, 2025 9:07:07 AM MST Ian via Digitalmars-d-learn wrote:Hello, What's the recommended D way to declare a string constant? Say, for example, for a path used inside a class constructor? I've seen const string, immutable, enum etc... Is this the place for these kinds of questions? Is there a D stack overflow?For strings, the way that you normally do constants is with enum, e.g enum foo = "dlang"; An enum like this is called a manifest constant. And you use manifest constants for most constants in D, with the caveat that for anything other than a string which involves an allocation, you probably don't want to use an enum. That's because enums are not variables, and their values are essentially copy-pasted wherever they're used. So, if you do something like enum foo = [1, 2, 3]; everywhere that you use foo, it'll be the same as if you used [1, 2, 3] directly. And because [1, 2, 3] allocates a new array, that means that each use of the enum allocates a new array. In such a situation, using a static variable would be better, e.g. static immutable foo = [1, 2, 3]; That does create a variable, so wherever you use foo, the array is sliced (so you get two arrays referring to the same piece of memory) rather than resulting in a new allocation. However, for strings, this is not an issue. This is because the compiler stores string literals directly in the program (in the ROM section of the binary on some OSes) and then slices that memory rather than allocating a new string every time you use the string literal. So, if you used "dlang" all over your program, you wouldn't get any allocations (unlike with [1, 2, 3]). And using an enum that's a string has the same result. So, typically, enums are used for constants which are strings - the same with int and other types which involve no allocations - whereas for other types of arrays, an immutable static variable is generally better. - Jonathan M Davis
Feb 24