www.digitalmars.com         C & C++   DMDScript  

D - why ~ for string concat? why not +?

reply neroden twcny.rr.com writes:
Just discovered D.  My first question is, why overload ~ and ~= for string
concatenation?  These are very odd choices.  The natural choices are of course
+ and +=.  If you don't want to do that because of a conflict with pointer
arithmetic, consider that pointer arithmetic without intermediate conversion to
integer types is a horrifying misfeature. :-)
May 01 2002
parent "Walter" <walter digitalmars.com> writes:
<neroden twcny.rr.com> wrote in message
news:aaova1$srd$1 digitaldaemon.com...
 Just discovered D.  My first question is, why overload ~ and ~= for string
 concatenation?  These are very odd choices.  The natural choices are of

 + and +=.  If you don't want to do that because of a conflict with pointer
 arithmetic, consider that pointer arithmetic without intermediate

 integer types is a horrifying misfeature. :-)

The reason was to save + and += on arrays to do an element-by-element add, such as a matrix add. Overloading + to mean "add" in once context and "concatenate" in another leads to ambiguity bugs like: "123" + 4 Does this produce the string: "1234" or the number: 127 ? There is no such ambiguity in D's approach, and so no need to invent an arbitrary, obscure, and forgettable rule about what to do in ambiguous cases.
May 01 2002