Morale of a story: ~ is the right choice for concat operator
rumbu
rumbu at rumbu.ro
Sat May 26 12:37:15 UTC 2018
On Saturday, 26 May 2018 at 10:08:35 UTC, Dukc wrote:
> On Saturday, 26 May 2018 at 09:01:29 UTC, rumbu wrote:
>> Sorry, but the mistake here is the fact that you wrongly
>> assume C behavior in C#.
>
> Yes it is. But that does not make differentiating concat and
> addition in language desing any less worthwhile. In car
> crashes, the mistake is usually made by a driver, but I know
> no-one who says safety belts aren't worthwhile.
>
>> Adding chars to an existing string will result in a string as
>> in the language specification.
>
> In fact I didn't make the mistake there. What surprised me was
> that adding two INDIVIDUAL chars result in a string. When op+
> is srictly for mathematical summing, there are no ambiquites.
Adding 2 individual chars in C# (and in D also) will result in an
int. Adding an int to a string will box the int and ToString()
will be called on the resulted object => result is a string. So
'a' + 'b' + "sssss" = 195 + "sssss" = 195.ToString() + "sssss" =
"195sssss".
Therefore your first example will work correctly if you convert
the int result back to char: (char)('a' + 'b') + "sssss" will
render the correct result.
Even if C# would use '~' instead of '+', you had a type
conversion problem, not an operator problem, you wrongly assumed
that adding two chars will result in a char, not an int. In the
hypothetically code 'a' + 'b' ~ "sssss" is also "195sssss".
More information about the Digitalmars-d
mailing list