Define overload math operators for types smaller than int #1624
-
Why have we to use cast to do basic math operations between bytes, short, and ushort? I think it will make things easier to define overload operators for those types.
this code will not even cause an overflow, but C# doesn't accept it. It only accepts:
|
Beta Was this translation helpful? Give feedback.
Replies: 16 comments
-
Because of hte language specification: https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#numeric-promotions
This could break huge swaths of programs out there. Any program that depends on the current numeric promotion logic would not have differing behavior. |
Beta Was this translation helpful? Give feedback.
-
I can't imagine that. The new change will allow to get rid off the casting, so, the existing code that uses that casting can live on. Do you have any examole for that break? |
Beta Was this translation helpful? Give feedback.
-
Sorry, i don't know what you're proposing then. Your title is:
So i assumed this meant defining operators like That would clearly be a breaking change. If that's not what you want, then what are you asking for? |
Beta Was this translation helpful? Give feedback.
-
@MohammadHamdyGhanem, as mentioned in https://github.com/dotnet/corefx/issues/30363#issuecomment-397050032, there is no way to guarantee that
As for others, operators, it isn't clear where you expect the cast to happen:
The rules today are very clear on what happens, and forcing you to add in an explicit cast ensures you understand those semantics. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi @tannergooding
|
Beta Was this translation helpful? Give feedback.
-
public static ushort implicit operator + (ushort x, ushort y)
=> ushort((int)x + (int)y); This would be a breaking change. You would get a different result for two ushorts added together. So: ushort a = ...;
ushort b = ...;
var c = a + b; //<-- this will change meaning. |
Beta Was this translation helpful? Give feedback.
-
Note: C# could have had operators like this in V1. Then things would have worked in the manner you want, and would have had well-defined semantics taht people could depend on. However, it went with numeric promotion in V1 (for many different reasons, but not actually relevant why right now). And changing htat, would definitely be breaking. So it can't change now, even if it would be beneficial for your needs. Note: if you want these sorts of operators, then you can define your own types that behave this way. For example: struct MHG_UShort
{
private readonly ushort val;
// define your operators
} Now you can use that instead of ushort and you can have the semantics you want for your own operators. You would not need to define numeric promotion for your own types. |
Beta Was this translation helpful? Give feedback.
-
It will not behave as i expect it (or anyone used to C# for the past 15 years). Numeric promotion is what i expect... :) |
Beta Was this translation helpful? Give feedback.
-
OK. Seems I need to write my own basic types, with implicit conversions to anf from intrinsic types! |
Beta Was this translation helpful? Give feedback.
-
This is def the recommendation when you want to carve out types that have differing semantics from what is already there :)
Any time!
So you need a declared type for C# to determine what to do? Isn't that what you get with |
Beta Was this translation helpful? Give feedback.
-
That is half the story, but waht brings this now, is a situation where Iwas sending (ushort + ushort) to a ushort param :). |
Beta Was this translation helpful? Give feedback.
-
Besides:
On top of them: |
Beta Was this translation helpful? Give feedback.
-
PR's welcome. I can guide you on how to do ths if you want. |
Beta Was this translation helpful? Give feedback.
-
I know you are that kind. Thanks. |
Beta Was this translation helpful? Give feedback.
-
I don't think keepign an unbounded list of work that is unlikely to get done is a good idea. If you'd like this feature, my recommendation woudl be to provide it. Otherwise, there's not much chance this will happen :) |
Beta Was this translation helpful? Give feedback.
-
I'm still stuck with Verex, and now it has a NuGet. I discovered some bugs and fixed them, so I can say V1.2 is stable until someone reports an issue. Now, I had to review two chapters about Collections and files (that should bave been done three month ago!) So, you can expect new issues about these two topics soon :) . |
Beta Was this translation helpful? Give feedback.
@MohammadHamdyGhanem, as mentioned in https://github.com/dotnet/corefx/issues/30363#issuecomment-397050032, there is no way to guarantee that
short + short
will produce an unsigned value (ushort
). As @karelz mentionedAs for others, operators, it isn't clear where you expect the cast to happen:
The rules today are very clear on what happens, and forcing you to add in an explicit cast ensures you understand…