Skip to content

INumber<TSelf> shouldn't inherit IUnaryNegationOperators<TSelf, TSelf>. #57307

@JustNrik

Description

@JustNrik

Description

I was fiddling around with the new INumber<TSelf> interface, but I found something that is definitely out-of-place.

INumber<TSelf> inherits IUnaryNegationOperators<TSelf, TSelf> which makes you implement this operator:

static abstract TSelf operator -(TSelf value);

At first sight it looks completely fine, but this is actually NOT fine for unsigned numbers. There's a really valid reason why uint returns long when you use the unary minus operator on it, or why ushort and byte returns int. And for this same reason, this interface shouldn't be part of INumber<TSelf> interface.

Suggestion

Move IUnaryNegationOperators<TSelf, TSelf> from INumber<TSelf> to ISignedNumber<TSelf>

In this way, IUnsignedNumber<TSelf> won't inherit such interface just to return a wrong data type or throw an exception.

Drawbacks

Breaking change but we are in previews so we're still in time, I personally consider it very necessary until its too late.
Another drawback is that now you have to implement IUnaryNegationOperators<TSelf, TResult> if you implement IUnsignedNumber<TSelf>, but that's how it should be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimeneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions