Skip to content

TimeSpan has all of the semantics of numbers, but none of the new interfaces #76225

Open
@davhdavh

Description

@davhdavh

Background and motivation

TimeSpan is fundamentally a long, and has most of the semantics of being a long.
Yet, when adding the IMinMaxValue<T>, INumberBase<T> and so on, none of them were applied to TimeSpan.
This prevents making things like Math.Min (if it had an overload that used the new interfaces) work on TimeSpans.

API Proposal

namespace System;

public class TimeSpan
        : IComparable,
          IComparable<TimeSpan>,
          IEquatable<TimeSpan>,
          ISpanFormattable,
          ISpanParsable<TimeSpan>,
//NEW
          IMinMaxValue<TimeSpan>,
          IAdditionOperators<TimeSpan, TimeSpan, TimeSpan>,
          IAdditiveIdentity<TimeSpan, TimeSpan>,
          IEqualityOperators<TimeSpan, TimeSpan, bool>,
          ISubtractionOperators<TimeSpan, TimeSpan, TimeSpan>,
          IUnaryPlusOperators<TimeSpan, TimeSpan>,
          IUnaryNegationOperators<TimeSpan, TimeSpan>,
          ISignedNumber<TimeSpan> //<-- problem with this, see below
{
...
}

API Usage

var min = Math.Min(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(1));

Alternative Designs

No response

Risks

A few semantics of the new interfaces does not really make sense for TimeSpan, e.g. TimeSpan can be negative, but the concept of NegativeOne does not really make much sense outside being a unary operator. and for some reason ISignedNumber<T> assumes the entire INumberBase<T> is applicable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions