www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - #define-like behavior

reply Jeremy <jtbx disroot.org> writes:
Hi, in C and C++ you can use #define to substitute a value in 
place of an identifier while preprocessing. If you initialize a 
new string and don't change its value after that, will the 
compiler substitute the string identifier with its value, like 
#define in C, or will it make a string in memory and refer to 
that?
Mar 13 2023
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 14 March 2023 at 05:47:35 UTC, Jeremy wrote:
 Hi, in C and C++ you can use #define to substitute a value in 
 place of an identifier while preprocessing. If you initialize a 
 new string and don't change its value after that, will the 
 compiler substitute the string identifier with its value, like 
 #define in C, or will it make a string in memory and refer to 
 that?
Manifest constants in D have a similar effect as #defined values, e.g.: ```d enum someVal = 10; writeln(someVal); ``` Here, the effect is the same as if you'd written `writeln(10)`. The difference is that with the preprocessor, it's a text replacement on the source before the compiler gets ahold of it, but in D the compiler handles the substitution internally. The same is true for string literals: ```d enum str = "Hello"; writeln(str); ``` String literals get special treatment from the compiler in that they are "interned". ```d auto s1 = "What's up?"; auto s2 = "What's up?"; ``` The literal "What's up?" *should* be stored in the binary only once, so both s1 and s2 will point to the same location. Substitute a manifest constant and the effect should be the same: ```d enum greeting = "What's up?"; auto s1 = greeting; auto s2 = greeting; ``` Just be aware that there's a consequence for array literals: ```d enum vals = [1, 2, 3]; auto a1 = vals; auto a2 = vals; ``` This allocates two dynamic arrays, not one. Everywhere you use `vals` it's just like using the literal directly, which usually means an allocation.
Mar 13 2023
next sibling parent Jeremy <jtbx disroot.org> writes:
On Tuesday, 14 March 2023 at 06:20:29 UTC, Mike Parker wrote:
 On Tuesday, 14 March 2023 at 05:47:35 UTC, Jeremy wrote:
 [...]
Manifest constants in D have a similar effect as #defined values, e.g.: [...]
Thanks a lot!
Mar 14 2023
prev sibling parent reply bomat <Tempest_spam gmx.de> writes:
Just out of curiosity:
Can you explain to me why this is called an `enum` although it's 
clearly not an enumeration?
Seems like a random abuse of a keyword...
Mar 15 2023
parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 15 March 2023 at 16:40:52 UTC, bomat wrote:
 Just out of curiosity:
 Can you explain to me why this is called an `enum` although 
 it's clearly not an enumeration?
 Seems like a random abuse of a keyword...
It's shorthand for defining an unnamed `enum` with a single member: ```d enum { myString = "hello" } ``` D lets you leave out the `{}` in this case, for convenience.
Mar 15 2023
parent bomat <Tempest_spam gmx.de> writes:
On Wednesday, 15 March 2023 at 19:27:19 UTC, Paul Backus wrote:
 It's shorthand for defining an unnamed `enum` with a single 
 member: ...
Ah, I see. Thanks!
Mar 15 2023
prev sibling parent Paul Backus <snarwin gmail.com> writes:
On Tuesday, 14 March 2023 at 05:47:35 UTC, Jeremy wrote:
 Hi, in C and C++ you can use #define to substitute a value in 
 place of an identifier while preprocessing. If you initialize a 
 new string and don't change its value after that, will the 
 compiler substitute the string identifier with its value, like 
 #define in C, or will it make a string in memory and refer to 
 that?
In D, you can get #define-like behavior by declaring the string as a [manifest constant][1], like this: ```d enum myString = "hello"; ``` For more tips on how to translate C preprocessor idioms to D, take a look at ["The C Preprocessor vs D"][2] in the ["Articles" section][3] of dlang.org. [1]: https://dlang.org/spec/enum.html#manifest_constants [2]: https://dlang.org/articles/pretod.html [3]: https://dlang.org/articles/index.html
Mar 13 2023