Skip to content

Determine recommended behavior of checked/unchecked operators for DateTime and similar types #67744

@tannergooding

Description

@tannergooding

As per #67714 (comment), there is some an open decision needed around how checked/unchecked operators work for types like DateTime. This likely will play into guidance we give customers for their own similar types as well.

The four basic options are:

  1. Don't implement the generic math interfaces on these types.
  2. Leave the behavior as is. Someone using these types in a generic context might experience exceptions for "overflow" (but not actually OverflowException) even when in unchecked.
  3. Implement the new unchecked operators on these types to not throw. This means someone using these in a generic context will have more consistent behavior regardless of the concrete type being used, but it yields an inconsistency between how the type behaves when using it directly vs generically.
  4. (3) and change the existing behavior of the existing operators to be non-throwing. That's a breaking change in that someone could easily be relying on that behavior to stop "bad things" from happening.

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 define operator checked + and users will be required to do that if implementing IAdditionOperators.

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 an unchecked context as they may not get an overflow where they did previously.

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Numericsdesign-discussionOngoing discussion about design without consensus

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions