Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to check for yourself whether the string is concatenated at compile time is to compile the code and examine the object file. Assuming your file is called <code>test.d</code>:</p> <pre><code>dmd -c test.d objdump test.o | grep -C3 "012345" </code></pre> <p>...should produce something like:</p> <pre><code>Contents of section .rodata: 0000 2d000000 00000000 00000000 00000000 -............... 0010 01000000 00000000 00000000 00000000 ................ 0020 30313233 34353637 38394142 43444546 0123456789ABCDEF 0030 4748494a 4b4c4d4e 4f505152 53545556 GHIJKLMNOPQRSTUV 0040 5758595a 00000000 00000000 00000000 WXYZ............ 0050 24000000 00000000 20000000 00000000 $....... ....... </code></pre> <p>(This is on Linux; on other platforms, you'll need different tools to inspect the object file.)</p> <p>If you change your <code>enum</code> to <code>const</code> or <code>string</code>, you will (probably) get no output: there will be no concatenated string for <code>grep</code> to find.</p> <p>But the compiler may concatenate strings at compile-time even when <code>enum</code> is not used. Consider this program:</p> <pre><code> import std.stdio; enum a = "Aaaa"; enum b = "Bbbb"; enum c = "Cccc"; void main() { enum x = a ~ b; const y = b ~ a; string z = a ~ c; writeln(x, y, z); } </code></pre> <p>Now, compile it, and examine the object file:</p> <pre><code>% dmd -c test2.d &amp;&amp; objdump -s test2.o | egrep "(Aaaa|Bbbb)" 0000 42626262 41616161 00000000 00000000 BbbbAaaa........ 0020 41616161 43636363 00000000 00000000 AaaaCccc........ 0040 41616161 42626262 00000000 00000000 AaaaBbbb........ </code></pre> <p>We see that <code>x</code>, <code>y</code> and <code>z</code> are all static literals. (Mark <code>a</code>, <code>b</code> and <code>c</code> as <code>const</code> instead of <code>enum</code>, and you may see different behaviour.) So, while <code>enum</code> is a guarantee of compile-time evaluation, the absence of <code>enum</code> doesn't prevent compile-time evaluation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload