www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template instantiation and string vs const char[]

reply "Jason den Dulk" <public2 jasondendulk.com> writes:
I have this piece of code:

template somet(R...) {
   static if (R.length)
     string somet = "[\n"~R[0]~"\n" ~ somet!(R[1..R.length]) ~ 
     string somet = "";

void main()

When I compile it, it generates the error message "static 
variable somet cannot be read at compile time". When I replace 
'string' with 'const char[]', it works.

Can anyone explain why this happens?

Sep 02 2014
parent "JR" <zorael gmail.com> writes:
On Wednesday, 3 September 2014 at 05:18:42 UTC, Jason den Dulk 

While not really answering your question, I believe the idiomatic 
way is to use enum here instead of string/char[].

Conjecture: strings are final but the symbol can be redirected to 
point to new arrays, such as results of concatenations. 
Concatenations create new and unique arrays upon each operation 
as each by definition cannot be altered. char[] can be *appended* 
to, technically retaining the same array and merely reallocating 
(potentially moving) to fit the extra content.

This sounds like something the compiler should be able to work 
around but apparently it doesn't.

Also, note that your check of (R.length) will only evaluate to 
false if the template is instantiated as somet!().
Sep 03 2014