Kill implicit joining of adjacent strings

Rainer Deyke rainerd at eldwood.com
Wed Nov 10 20:51:38 PST 2010


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.


-- 
Rainer Deyke - rainerd at eldwood.com


More information about the Digitalmars-d mailing list