Deprecation of implicit string concatenation

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 2 04:14:31 PDT 2017


On Sunday, April 02, 2017 10:55:22 Johan Engelen via Digitalmars-d wrote:
> On Sunday, 2 April 2017 at 10:05:57 UTC, Jonathan M Davis wrote:
> > On Sunday, April 02, 2017 11:47:52 Jacob Carlborg via
> >
> > Digitalmars-d wrote:
> >> On 2017-04-02 11:22, Johan Engelen wrote:
> >> > Since 2.072, implicit string concatenation is deprecated
> >> > [1]. Can someone give me a link to the discussion about this?
> >> >
> >> > I am wondering about the language spec changes involved.
> >> >
> >> > ```
> >> >
> >> >   "abc"
> >> >   "def"
> >> >
> >> > ```
> >> > means something different than
> >> > ```
> >> >
> >> >   "abc"
> >> >   ~ "def"
> >> >
> >> > ```
> >> > right? (for example because opBinary!(“~”) can be overloaded)
> >>
> >> It's not possible to overload operators for the built in
> >> types, or am I missing something?
> >
> > No, it's definitely not possible to overload operators on
> > built-in types.
>
> This is not about overloading of built-in types. It is about the
> code text `"abc" ~ "def"` and `"abc" "def"` not being equivalent.
>
> I am afraid the deprecation happened without careful thought
> behind it, because the deprecation text shows only the simplest
> of cases. It does not show a case where the ~ operator is used as
> part of a larger expression and together with operator
> overloading.
>
> ```
> import std.stdio;
>
> class A {
>    A opBinary(string op)(string rhs) {
>      writeln(rhs);
>      return this;
>    }
> }
>
> void main() {
>    A a = new A();
>
>    a ~ "abc" ~ "def";
>
>    a ~ "abc"  "def";
> }
> ```

True, those two statements are slightly different. I hadn't thought of that
case. However, all you have to do is put parens around the string
concatenation, and it's not an issue. I don't see why it would be a big deal
at all. At most, it simply means that anyone changing code that purposely
used implicit string concatenation is going to need to be aware of how
there's a difference when dealing with a user-defined type. For the vast
majority of cases, there is no difference whatsoever, and in some - if not
most - cases, it'll actually catch bugs in the code, because most people
don't use implicit string concatenation on purpose.

I fail to see why deprecating implicit string concatenation would be a
problem. Most of us agree that it's an anti-feature that makes it harder to
catch bugs related to string literals, and it's perfectly possible to
concatenate string literals explicitly.

And if the problem is the deprecation message, well, you can only put so
much in a deprecation message without making it too long. But if you have a
suggestion for how the message could be better, feel free to open up an
enhancement request for it over even to create a PR.

- Jonathan M Davis




More information about the Digitalmars-d mailing list