May 26, 2018
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.
May 26, 2018
On Friday, 25 May 2018 at 08:27:30 UTC, Dukc wrote:
> If you add two characters, it interprets it as a concatenation that results in a string with two charactes.
...
> Now, if I were programming in D, this would not have happened. Using + always means an addition.

I don't think it makes sense to allow adding two characters - the second operand should be an integer type. Why have `byte` in the language if `char` works like an integer? Ideally ops like addition would allow one operand to be a character type, but require the other operand to be an integer - that is a useful operation, unlike adding '+' to 'Z'.
May 26, 2018
On Saturday, 26 May 2018 at 11:04:44 UTC, Nick Treleaven wrote:
>
> I don't think it makes sense to allow adding two characters - the second operand should be an integer type.

So it would behave like pointer arithmetic. Sounds sound. Not for D because of the C semantic similarity requirement but for some other aspiring language.


May 26, 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".


May 26, 2018
On Saturday, 26 May 2018 at 12:37:15 UTC, rumbu wrote:
> 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".

I had to go back and check. Yes, it appears I screw up here. Sorry.

Sigh, this is one of the cases whereI wish I could edit my posts.
1 2
Next ›   Last »