Kill implicit joining of adjacent strings

Vladimir Panteleev vladimir at thecybershadow.net
Wed Nov 10 21:03:32 PST 2010


On Thu, 11 Nov 2010 06:51:38 +0200, Rainer Deyke <rainerd at eldwood.com>  
wrote:

> On 11/10/2010 19:34, bearophile wrote:
>> Do you seen anything wrong in this code? It compiles with no errors:
>>
>> enum string[5] data = ["green", "magenta", "blue" "red", "yellow"];
>> static assert(data[4] == "yellow");
>> void main() {}
>>
>>
>> Yet that code asserts.
>
> Wait, what?  That's a static assert.  How can it both assert and compile
> with no errors?
>
> As it turns out, the joining of adjacent strings is a critical feature.
>  Consider the following:
>   f("a" "b");
>   f("a" ~ "b");
> These are /not/ equivalent.  In the former cases, 'f' receives a string
> literal as argument, which means that the string is guaranteed to be
> zero terminated.  In the latter case, 'f' receives an expression (which
> can be evaluated at compile time) as argument, so the string may not be
> zero terminated.  This is a critical difference if 'f' is a (wrapper
> around a) C function.

Wait, so you imply that if I pass 2+2 as a parameter to a function with  
lazy parameters, the compiler should not do partial expression calculation  
and should actually add up 2+2 at runtime? Because, AFAICS, concatenating  
two string literals should be no different from adding two numbers.

-- 
Best regards,
  Vladimir                            mailto:vladimir at thecybershadow.net


More information about the Digitalmars-d mailing list