July 24, 2017
On 7/22/17 6:44 AM, Walter Bright wrote:
> They are supposed to match C. But they don't for unary operators + - ~, and as far as I can tell never did.

My opinion is that it should behave unsurprisingly.

It's very surprising behavior for:

ubyte x = 1;
int y = -x;

To make y = 255.

My recommendation:

1. issue a warning for using any of those operators an unsigned type without an unambiguous cast. In other words:

ubyte x = 1;
-x; // warning
cast(ubyte)-x; // OK, not changing size
cast(int)-x; // warning
-cast(int)x; // OK

2. Change the warning to a deprecation.
3. Change to an error

optionally, you could finally implement the C behavior after this and remove the error, but I'm actually thinking that the number of times this comes up is probably so few that it's worth requiring the cast.

Note, the negation of byte.min or short.min is not as surprising as the negation of unsigned types, as int or long have the same behavior, and it's a well-known corner case.

Also note, you did not include char/wchar in your list of affected types, though they have the same behavior with those operators as ubyte and ushort.

-Steve
July 24, 2017
On Saturday, 22 July 2017 at 10:44:04 UTC, Walter Bright wrote:
> 1. Fix it so it matches C, as has been generally promised. Fixing it will break existing code such as:

If D was my language I'd fix it, since it's a bug.

D's fluidity and effortlessness comes from a lot of small compounding convenience features.
The converse is true as well. A lot of small annoyances accumulate into frustration.
July 24, 2017
Yes, I think a switch to enable the correct behavior, and a warning when the cases are encountered and the switch isn't thrown, is likely the best approach.
July 24, 2017
On 7/24/2017 2:07 PM, Walter Bright wrote:
> Yes, I think a switch to enable the correct behavior, and a warning when the cases are encountered and the switch isn't thrown, is likely the best approach.

https://github.com/dlang/dmd/pull/7013
July 25, 2017
On 7/24/17 6:46 PM, Walter Bright wrote:
> On 7/24/2017 2:07 PM, Walter Bright wrote:
>> Yes, I think a switch to enable the correct behavior, and a warning when the cases are encountered and the switch isn't thrown, is likely the best approach.
> 
> https://github.com/dlang/dmd/pull/7013

Yes, that should be workable.

I'm looking at some of the code that you have to update, and I'm wondering if ~ should still operate the way it does. While - on an unsigned is somewhat clear that you want a negative (and therefore a different type), ~ on an unsigned actually makes sense for the same type.

However, if C operates this way, this would be a huge trap for people who are porting code.

-Steve
July 25, 2017
On 7/25/2017 6:43 AM, Steven Schveighoffer wrote:
> However, if C operates this way, this would be a huge trap for people who are porting code.

That's right. Also for C/C++ programmers who are used to how C works.

1 2
Next ›   Last »