-
Notifications
You must be signed in to change notification settings - Fork 387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic math for UnitsNet in .NET 7 #1164
Generic math for UnitsNet in .NET 7 #1164
Conversation
Awesome! I will have to take a closer look later. |
…s. Build also .NET 7. Ignore nullability warnings for now
9d77f6f
to
b343f79
Compare
- Add `Zero` static abstract property, already implemented by all quantities. - Remove redundant `IEqualityOperators` constraint, covered by `IComparisonOperators`. - Change `IDivisionOperators` to divide by number instead of TSelf, needed for average calculation. Got error trying to keep both IDivisionOperators declarations: ``` Interface 'UnitsNet.IArithmeticQuantity<TSelf,TUnitType,TValueType>' cannot implement both 'System.Numerics.IDivisionOperators<TSelf,TValueType,TSelf>' and 'System.Numerics.IDivisionOperators<TSelf,TSelf,TValueType>' because they may unify for some type parameter substitutions ```
Add generic math implementations and tests to test out the new interfaces: - Sum - Average
Inspired by article: https://www.meziantou.net/how-to-use-nullable-reference-types-in-dotnet-standard-2-0-and-dotnet-.htm Related to #1164 ### Changes - Add `NullableAttributes.cs` via `Directory.Build.props` to shim annotation attributes for netstandard2.0 and older .NET versions - Fix nullable warnings, observed when multitargeting net7.0 + netstandard2.0 - Add `[NotNullWhen(true)]` to all `Try` methods - Remove redundant `!` nullable suppressions
I fixed nullable annotations in #1175 and added some initial extension methods Some changes made to the PR:
Please review my commits and take a look whether we should expand on the extension methods or make other tweaks, but I think this is more or less ready to go. There are definitely some paper cuts with regards to having units in the mix, such as addition picking the left hand side unit so my initial attempt at Average() resulted in the base unit instead of the unit in the collection. We'll just have to test this out more and get a better feel for it, but it does seem to kind of work. |
Codecov ReportBase: 88% // Head: 87% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## master #1164 +/- ##
=======================================
- Coverage 88% 87% -1%
=======================================
Files 319 321 +2
Lines 31557 31679 +122
=======================================
+ Hits 27858 27867 +9
- Misses 3699 3812 +113
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Looks good, I think 👍 Regarding the operators in the *.extra.cs files, that could be tackled easily by describing the dependencies (operations) between different quantities in the quantity JSON files, and then the operator overloads and the generic math interfaces for the quantity structs could also be automatically generated. But that's a topic for another time 😄 |
Thanks for the great work! The nuget should be out to play with shortly. |
…hange. Intoduced with a change from (angularsen#1164) thats why I removed one space from QuantityGenerator.cs
Related #1200 In the PR adding generic math (#1164) @AndreasLeeb states: > Regarding the operators in the *.extra.cs files, that could be tackled easily by describing the dependencies (operations) between different quantities in the quantity JSON files, and then the operator overloads and the generic math interfaces for the quantity structs could also be automatically generated. But that's a topic for another time 😄 I decided to give this a shot. `UnitRelations.json` contains relations extracted from the existing *.extra.cs files. I decided on a new file because multiplication is commutative and I didn't want to duplicate these in the individual quantity JSON files, or risk missing one or the other, so it's best to define them once in one place. The generator handles this by generating two operators for a single multiplication relation. The relations format uses the quantities method names. This is a bit unfortunate, but it's the best I could come up with without making the CodeGen project depend on UnitsNet, which would create a bit of a chicken/egg problem. This is not unheard of (self-hosted compilers) but I wanted to keep it simple for now. The generated code enables the removal of 44 *.extra.cs files, and the 17 remaining contain much less code. --------- Co-authored-by: Andreas Gullberg Larsen <[email protected]>
Hi,
since it's already been quite some time since #984 and as #1129 shows that the topic is of interest, I also gave it a shot.
So far, it's been a surprisingly minimally invasive undertaking.
Changes so far:
Still to do:
Pain points: