-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Determine recommended behavior of checked/unchecked operators for DateTime and similar types #67744
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
CC. @stephentoub |
Tagging subscribers to this area: @dotnet/area-system-numerics Issue DetailsAs per #67714 (comment), there is some an open decision needed around how The four basic options are:
More generically users will have types that expose simply The user always has the option of preserving the existing public surface and explicitly implementing the interface. This allows a preservation of the current behavior whenever interacting with the concrete type and allows the behavior in an appropriately constrained generic context to work differently. The user also has the option of implementing the interfaces implicitly and taking a behavioral break for existing dependents. If the behavior was universally allow overflow (unchecked) then the user may see a behavioral break on recompile for code that uses the operator in a
|
I'll take this to API review as part of this next round of generic math. Would be good to get some initial thoughts from interested @dotnet/fxdc members beforehand, however. My own leaning is that 3 is the least breaking and potentially the most useful overall. Utilizing the code from a generic context is an entirely new concept here and so there won't be users relying on an existing behavior. There is no risk of "breaking" existing users and users may even expect a different/overall more consistent behavior generically speaking. There would be an observable difference between Users would then be free to decide if 4 (making the exposed public surface area match the generic context behavior) is an additional step worthwhile for the types in question. It would be a behavioral (but non source or binary breaking) change; but that's largely something that only the implementors of a given type can decide if its worthwhile or not. 2 would be the next thing I'd lean towards. It would just clarify that there should be no expectation that |
This sounds to me like the correct choice can only be made when looking at concrete use cases as examples. This kind of choice is very hard to make theoretically. So... are there known use cases for generic math on time types? I'm sure there are, and it would inform the decision greatly if plausible situations were looked at. And once the choice is made, it's final for compat reasons. |
That said, number 4 (changing behavior to overflow) seems to be very undesirable. It's not just breaking but it makes time bugs more likely and harder to diagnose. Overflowing a time value intentionally is, I think, a 0% use case that nobody wants ever. |
This covers more than just As for whether or not overflow is appropriate, it really depends. If you're tracking say One could argue the same for |
The feeling in the room is a mix of (1) and (3).
|
Closing as the relevant APIs have been updated to work as expected. |
@tannergooding, I see we lumped |
We opted for removing all the new operator interfaces from the non-numeric types until more concrete scenarios were presented around their usage and versioning over time. |
As per #67714 (comment), there is some an open decision needed around how
checked
/unchecked
operators work for types likeDateTime
. This likely will play into guidance we give customers for their own similar types as well.The four basic options are:
More generically users will have types that expose simply
operator +
today and that may universally allow (unchecked) or disallow (checked) overflow. Starting with C# 11, users can now also defineoperator checked +
and users will be required to do that if implementingIAdditionOperators
.The user always has the option of preserving the existing public surface and explicitly implementing the interface. This allows a preservation of the current behavior whenever interacting with the concrete type and allows the behavior in an appropriately constrained generic context to work differently.
The user also has the option of implementing the interfaces implicitly and taking a behavioral break for existing dependents. If the behavior was universally allow overflow (unchecked) then the user may see a behavioral break on recompile for code that uses the operator in a
checked
context as that may now throw for overflow where it didn't previously. If the behavior was universally disallow overflow (checked), then the user may see a behavioral break for already compiled assemblies or on recompile for code that users the operator in anunchecked
context as they may not get an overflow where they did previously.The text was updated successfully, but these errors were encountered: