Skip to content

Conversation

@Manonox
Copy link

@Manonox Manonox commented Jan 15, 2026

Fixes: #908

TL;DR: Adds I?Operators interfaces to vector types, so that you can do math on them as generics:

public TResult Add<TSelf, TOther>(TSelf c1, TOther c2) where TSelf: IAdditionOperators<TSelf, TOther, TResult> {
    return c1 + c2; // Will not work for vectors without the interface
}

Also added IAdditiveIdentity and IMultiplicativeIdentity while I am at it

As boonoos - cleaned up some minor stuff:

  • Added missing operators (and their ifaces) that are present in "neighboring" types
  • Moved indexing and the #region directive in Vector4 to match Vector2 and Vector3
  • Removed a couple ins from operator arguments (incompatible with interfaces, (if there was a reason to pass vector3s by reference instead of by value - tell me)

Copilot AI review requested due to automatic review settings January 15, 2026 07:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds System.Numerics interfaces to vector wrapper types (Vector2, Vector3, Vector4, Vector2Int, Vector3Int) to enable generic math operations. The changes make these types compatible with .NET's generic math interfaces introduced in recent .NET versions.

Changes:

  • Added System.Numerics operator interfaces (IAdditionOperators, IMultiplyOperators, etc.) to all vector types
  • Added identity interfaces (IAdditiveIdentity, IMultiplicativeIdentity) with implementations
  • Removed in modifiers from operator parameters to maintain interface compatibility
  • Reorganized code in Vector4.cs to match the structure of Vector2/Vector3

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Vector4.cs Added numerics interfaces, removed duplicate operators, reorganized indexer/region placement
Vector3.cs Added numerics interfaces, removed in modifier from division operator
Vector2.cs Added numerics interfaces and identity properties
Vector3Int.cs Added comprehensive numerics interfaces including cross-type operations
Vector2Int.cs Added comprehensive numerics interfaces, reordered operators, added missing operations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@handsomematt
Copy link
Member

What the fuck

These don't seem to be on System.Numerics.Vectors?

@lolleko
Copy link
Contributor

lolleko commented Jan 15, 2026

Removing the in keyword is a breaking change.
We will likely remove these soo, with a proper upgrade path but until then i would definitely not merge this.

Also would like to see an actual use case where this is needed.

@Manonox
Copy link
Author

Manonox commented Jan 17, 2026

Removing the in keyword is a breaking change. We will likely remove these soo, with a proper upgrade path but until then i would definitely not merge this.

Yeah, it is very much a breaking change, at least I am hopeful that ppl don't just throw Vector2 * Vector3 around

Also would like to see an actual use case where this is needed.

I am updating my nodes
(https://sbox.game/manonox/nodeboxshowcase)
And I need a way to do math in a more optimized way, if you put an IAdditionOps constraint on T in something like Add<T> you can use proper addition and it'll probably be the most optimized way of doing it
ActionGraph just uses LINQ expressions which peasants like me can't access/use.
The only remaining option is to make an "if tower"/switch or a hashmap with some funcs.
(Non-updated version of Nodebox has a lot of if towers right now)

@Manonox
Copy link
Author

Manonox commented Jan 17, 2026

@lolleko , tl;dr, it's either interfaces, op_?'s in the TypeLibrary or LINQ expressions
Also, should probably add interfaces to these too:
Angles, Rotation, Transform, Color, Rect, RectInt, Margin, TimeSince, RealTimeSince, TimeUntil and RealTimeUntil
If this is gonna be picked up at some point, ping me or do it yourself before merging

@Manonox
Copy link
Author

Manonox commented Jan 17, 2026

If there are any other alternatives I would really like to know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Operator Interfaces for Vector2, Vector3, Vector4, Vector2Int and Vector3Int

3 participants