Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>char[]</code> casts to <code>const char[]</code> implicitly. No cast is needed.</p> <pre><code>auto foo(const char[]) {...} auto foo(const(char)[]) {...} </code></pre> <p>will both accept any variation of mutability of <code>char[]</code> - <code>char[]</code>, <code>const(char)[]</code>, <code>immutable(char)[]</code>, <code>const char[]</code>, and <code>immutable char[]</code>. Both mutable and <code>immutable</code> implicitly convert to <code>const</code> in general.</p> <p><strong>EDIT (addition after code added to question):</strong></p> <p>Actually, I'm shocked that your second line compiles. This has nothing to do with <code>const</code> vs mutable. This has to do with compile-time vs runtime. The strings that you pass to a <code>mixin</code> must be known at compile time. Normally, that means using a function to return the string (in which case, it's called as part of CTFE) or using an enum (which would be known at compile time). Variables aren't known at compile time and therefore can't normally be used. However, you lucked out with your <code>const</code> version, because the compiler knew that the value would never change, and the value was directly initialized, so it knew what it was, and it let you get away with it. If you had had</p> <pre><code>const char[] tab2 = foo(); </code></pre> <p>and <code>foo</code> had returned the string that you wanted to use, that <code>mixin</code> would have failed to compile just like the first on.</p> <p>Strings for mixins should normally either be directly passed to the mixin, returned from a function (which is called as part of CTFE), or be an enum.</p> <p>If you want to put the contents of a file in a <code>mixin</code>, then you need to use a string import. e.g.</p> <pre><code>mixin(import("foo.txt")); </code></pre> <p>And you'll need to tell the compiler what directory to find <em>foo.txt</em> in using the <code>-J</code> flag.</p>
    singulars
    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. 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.
 

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