Skip to content

✨ Experiment with Generic Math in .NET6 - #984

Closed
angularsen wants to merge 2 commits into
masterfrom
agl/generic-math
Closed

✨ Experiment with Generic Math in .NET6#984
angularsen wants to merge 2 commits into
masterfrom
agl/generic-math

Conversation

@angularsen

@angularsenangularsen commented Nov 2, 2021

Copy link
Copy Markdown
Owner

Experimenting with https://devblogs.microsoft.com/dotnet/preview-features-in-net-6-generic-math/

Example:

publicinterfaceIQuantity<TQuantity>:IAdditionOperators<TQuantity,TQuantity,TQuantity>,IParseable<TQuantity>,IComparisonOperators<TQuantity,TQuantity>,IAdditiveIdentity<TQuantity,TQuantity>whereTQuantity:IQuantity<TQuantity>{staticabstractTQuantityZero{get;}}

Allows us to do:

voidFoo<TQuantity>(TQuantityleft,TQuantityright)whereTQuantity:IQuantity<TQuantity>{WriteLine(left.CompareTo(right));WriteLine(left+right);WriteLine(TQuantity.Parse("25 m",null);}TQuantitySum<TQuantity>(IEnumerable<TQuantity>items)whereTQuantity:IQuantity<TQuantity>{returnitems.Aggregate(TQuantity.Zero,(acc,item)=>acc+item);}

Full sample output:

Foo(1 Meter, 10 Centimeter)
---
1 <== left.CompareTo(right)
1.1 Meter <== left + right
25 Meter <== TQuantity.Parse("25 m", null)
10 Meter <== Sum [1,2,3,4] array of meters

@stale

staleBot commented Jan 3, 2022

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stalestaleBot added the wontfix label Jan 3, 2022
@tmilnthorp

Copy link
Copy Markdown
Collaborator

Love it. It doesn't seem like there is a way to make it backwards compatible to netstandard, etc. though :(

@stalestaleBot removed the wontfix label Jan 26, 2022
@angularsenangularsen added the pinned Issues that should not be auto-closed due to inactivity. label Jan 26, 2022
@tmilnthorptmilnthorp mentioned this pull request Jan 31, 2022
@angularsen

Copy link
Copy Markdown
OwnerAuthor

Merged by #1164

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

Labels

pinnedIssues that should not be auto-closed due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@angularsen@tmilnthorp