Last active
June 6, 2021 07:55
-
-
Save rstropek/4148ab8042328489d93af3fdcec11441 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
ReadOnlySpan<Vector2d> vectors = stackalloc Vector2d[] { new(1d, 1d), new(2d, 2d), }; | |
Console.WriteLine(AddAll(vectors)); | |
static T AddAll<T>(ReadOnlySpan<T> addables) where T: IAddable<T> | |
{ | |
var result = T.Zero; | |
foreach (var a in addables) result += a; | |
return result; | |
} | |
interface IAddable<T> where T : IAddable<T> | |
{ | |
static abstract T Zero { get; } | |
static abstract T operator +(T t1, T t2); | |
} | |
record struct Vector2d(double X, double Y) : IAddable<Vector2d> | |
{ | |
public static Vector2d operator +(Vector2d first, Vector2d second) | |
=> new(first.X + second.X, first.Y + second.Y); | |
public static Vector2d Zero => new(0d, 0d); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment