Deprecation of implicit string concatenation

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 2 03:05:57 PDT 2017


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.
Overloaded operators must be member functions. You can't even add an
overloaded operator to a user-defined type that you don't control via UFCS,
let alone add an overloaded operator to a built-in type. So, there's zero
risk of anyone messing with the semantics of "abc" ~ "def". And I'm not
aware of any difference in semantics between

"abc"
"def"

and

"abc" ~
"def"

In theory, the compiler could choose to do the concatenation at runtime in
the second case, but it does it at compile time. And that's the only
possible difference that I'm aware of that there even could be.

As to why the change was made, it was made because of all of the bugs caused
by missing a comma or ~ when dealing with string literals. Those bugs are
caught if implicit concatenation is illegal, and it's not much more verbose
to have to use ~. Also, the intention of the code is clearer. But the main
motivator is to catch bugs. It's a lot like how having if statements or
loops with ; as their body is illegal in D. You can use {} instead, so you
don't lose any expressiveness, but it prevents some common bugs which can be
very easy to miss and hard to find.

- Jonathan M Davis




More information about the Digitalmars-d mailing list