UnitsNet icon indicating copy to clipboard operation
UnitsNet copied to clipboard

Fully embrace INumber and generic math of .NET 7

Open bitbonk opened this issue 2 years ago • 5 comments

.NET 7 will introduce the INumber<TSelf> interface (along with accompanying with C# 11 features) that make a feature called generic math possible.

I think this would be a perfect fit for the quantities of UnitsNet and could potentially make the usage way more intuitive, flexible and overall enjoyable.

Please consider to fully embrace INumber<TSelf> and generic math in a future version of UnitsNet.

Here is a short introduction to this feature set by @MadsTorgersen (first 25 minutes): Where's C# headed? - Mads Torgersen - NDC Copenhagen 2022 Official documentation: https://docs.microsoft.com/en-us/dotnet/standard/generics/math

bitbonk avatar Aug 30 '22 16:08 bitbonk

The biggest question is the supported deployments. Do we abandon netstandard/netframework support just for generic math? Neither are EOL yet.

tmilnthorp avatar Aug 30 '22 16:08 tmilnthorp

You could only support it a future mayor version without downlevel support. Also, would multitargeting be an option?

bitbonk avatar Aug 30 '22 16:08 bitbonk

We already do, but without too many #ifs. I fear that the API support for generic math vs precompiled lambdas would be difficult to maintain with equivalent functionality. But definitely worth a try.

tmilnthorp avatar Aug 30 '22 16:08 tmilnthorp

My guess would be that IQuantity<TUnitType> would need to derive from INumber<TSelf> somehow, right?

bitbonk avatar Aug 30 '22 17:08 bitbonk

@bitbonk An early prototype here to explore how it can be used. #984

I haven't yet looked into what the changes were compared to the experimental interfaces at the time, but I assume it is in the ballpark similar.

angularsen avatar Aug 30 '22 18:08 angularsen

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.

stale[bot] avatar Nov 12 '22 22:11 stale[bot]

Some support for generic math was added in #1164 , it can be extended further.

angularsen avatar Feb 17 '23 19:02 angularsen

Some support for generic math was added in #1164 , it can be extended further.

Awesome! What version of UnitsNet has generic math included?

bitbonk avatar Feb 17 '23 19:02 bitbonk

v5 has it 👍 There are a couple of extension methods Sum() and Average() in GenericMathExtensions to help use it.

angularsen avatar Feb 17 '23 20:02 angularsen

https://github.com/angularsen/UnitsNet/pull/1164/files#diff-88adba83458faa7e4ec81b932cb85045c84f3b9e1b077bbc647167910cd07efa

angularsen avatar Feb 17 '23 20:02 angularsen