Implicit string literal concatenation will die [Was: Is implicit string literal concatenation a good thing?]

bearophile bearophileHUGS at lycos.com
Sun Feb 22 04:01:02 PST 2009


Frank Benoit Wrote:

>     static string[] KEYWORDS = [ "abstract", "alias", "align", "asm",
>         "assert", "auto", "body", "bool", "break", "byte", "case",
> ...
>         "with", "~this" ];
> 
> There is a comma missing : "uint" "ulong"

In such situations I often let the language split my string for me, it reduces noise:

auto keywords = "abstract alias align asm
                 assert auto body bool break byte case
                 cast catch cdouble cent cfloat char class
                 const continue creal dchar debug default
                 delegate delete deprecated do double else
                 enum export extern false final finally
                 float for foreach foreach_reverse function
                 goto idouble if ifloat import in inout
                 int interface invariant ireal is lazy long
                 mixin module new null out override package
                 pragma private private: protected protected:
                 public public: real return scope short
                 static struct super switch synchronized
                 template this throw true try typedef typeid
                 typeof ubyte ucent uint ulong union unittest
                 ushort version void volatile wchar while
                 with ~this".split();

You can also put one keyword for each line, or put them in better formatted columns.

If the strings may have spaces too inside then, then I put each string in a different line, and then split according to the lines with std.string.splitlines() (or str.splitlines() in Python).

Implicit string literal concatenation is a bug-prone anti-feature that is a relic of C language that doesn't have a nice string concatenation syntax. In D (and Python, etc) it's bad.
Months ago I have suggested to remove it and turn adjacent string literals into a syntax error (to "solve" the back-compatibility with ported C/C++ code).

Brad Roberts:

>In D, it's only marginally so, but not completely useless.  It can still be used to break a really long string literal into parts.<

In such situations you can put a ~ at the end of each part. Explicit is better than implicit :-)

Bye,
bearophile



More information about the Digitalmars-d mailing list