Kill implicit joining of adjacent strings

bearophile bearophileHUGS at lycos.com
Wed Nov 10 18:34:07 PST 2010


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. it's an excellent example of why a sloppy compiler/language sooner or later comes back to bite your ass.
I've recently had another bug caused by automatic joining of adjacent strings. I think this is the 3rd I have found in my D code. This is enough.

In C the joining of adjacent strings is sometimes useful, but explicit is better than implicit, and D has a short and good operator to perform joining of strings, the ~, and D strings are allowed to span multi-lines.

C code ported to D that doesn't put a ~ just raises a compile time error that's easy to understand and fix. So this doesn't change the meaning of C code, just asks the programmer to improve the code, and the change requires is fully mechanical.

The compiler need to be able to perform the joining at compile time, so zero run-time overhead is present. The result is the same as before, it's just a syntax change.

My bug report has more info and a partial patch:
http://d.puremagic.com/issues/show_bug.cgi?id=3827

Despite Walter seems to ignore C#, C# is a very well designed language, polished, and indeed it refuses automatic joining of adjacent strings:

public class Test {
    public static void Main() {
        string s = "hello " "world";
    }
}

That C# code gives the error:

prog.cs(3,35): error CS1525: Unexpected symbol `world'
Compilation failed: 1 error(s), 0 warnings

This is one of the about twenty little/tiny changes I am waiting for D.

So please kill automatic joining of adjacent strings in D with fire.

Thank you,
bearophile


More information about the Digitalmars-d mailing list