UnitsNet
UnitsNet copied to clipboard
Fully embrace INumber and generic math of .NET 7
.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
The biggest question is the supported deployments. Do we abandon netstandard/netframework support just for generic math? Neither are EOL yet.
You could only support it a future mayor version without downlevel support. Also, would multitargeting be an option?
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.
My guess would be that IQuantity<TUnitType> would need to derive from INumber<TSelf> somehow, right?
@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.
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.
Some support for generic math was added in #1164 , it can be extended further.
Some support for generic math was added in #1164 , it can be extended further.
Awesome! What version of UnitsNet has generic math included?
v5 has it 👍 There are a couple of extension methods Sum() and Average() in GenericMathExtensions
to help use it.
https://github.com/angularsen/UnitsNet/pull/1164/files#diff-88adba83458faa7e4ec81b932cb85045c84f3b9e1b077bbc647167910cd07efa