On 28.02.2016 00:29, mahdi wrote:
I read this criticism about D on Reddit and it claims that you cannot
use strings in mixins. Can you please elaborate about this and the
reason behind it?

QUOTE:
Look at strings: they are defined as immutable(char []).

immutable(char)[] actually

"immutable" as
in "you could put it in ROM", ... CTFE can't see strings correctly
(can't be determined at compile time). So rather than fix the compiler
so that strings can be used in mixins in the way that people expect it
to work even if that means making them something other than
immutable(char []), they decided to start using enums where strings
actually go. Seriously: take the same code and replace 'string "foo"'
with 'enum "foo"' and it starts to work!
END QUOTE

I can only guess that something like this is the perceived problem:

----
string hello(string who) {return "hello " ~ who;}
void main()
{
    string w = "world";
static string hw = hello(w); /* Error: variable w cannot be read at compile time */
}
----

The poster seems to be puzzled as to why the compiler refuses to see that `w` is a constant. It's even marked "immutable"!

As noted above, `w` is not immutable, though. Only the elements are. Also, `immutable` does not mean that the value is a compile time constant. Immutable values can be constructed at run time, and then they can't be used at compile time, obviously. `enum` values, on the other hand, are compile time constants.

Reply via email to