Repository navigation
Generate units as UnitOf<TQuantity>, such as LengthUnits.Meter - #1741
Draft
tmilnthorp wants to merge 9 commits into
Draft
tmilnthorp wants to merge 9 commits into
tmilnthorp wants to merge 9 commits into
Conversation
Each yield-return method compiles to a hidden iterator class. Returning an array removes 129 of those classes (-194 KB in the net10.0 DLL) and lets QuantityInfo size its unit array up front. The return type is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
UnitOf<TQuantity> describes a unit of a quantity by its names, base units and conversions, without a unit enum value. Quantities can be converted to it with As(UnitOf<TQuantity>), and created from a value in it with QuantityInfo.From(value, UnitOf<TQuantity>), which returns the quantity in its base unit. Since the unit is tied to its quantity, using it with another quantity doesn't compile. This makes it possible to use units that UnitsNet doesn't define, without a unit enum value or any setup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Base units are made of unit enum values, which a unit that UnitsNet doesn't define has none of, and nothing reads them yet. UnitOf still implements IUnitDefinition, with BaseUnits.Undefined as an explicit implementation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
The constructor took the conversion from the base unit, so a furlong needed the fraction 1000/201168. Taking the value of one unit in the base unit instead lets it be written as 201.168, as units are usually described. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
A unit that UnitsNet doesn't define can still be made of the base units it does, such as feet per day of speed. New constructor overloads take its base units, which UnitOf now exposes; the others leave them undefined. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Each quantity gets a static class of its units, such as LengthUnits.Meter, as UnitOf instances with their names, base units and conversions. The default unit mappings are created from them, so the data is generated once. Quantities can be converted to and from them with As and QuantityInfo.From like custom units, and a built-in unit converts through its UnitInfo, respecting the configuration. UnitOf<TQuantity> now derives from a non-generic UnitOf, which holds its data, so its code isn't compiled again for each quantity. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1741 +/- ##
=======================================
- Coverage 98% 98% -1%
=======================================
Files 515 645 +130
Lines 24092 24137 +45
=======================================
+ Hits 23692 23736 +44
- Misses 400 401 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Step 1 of the proposal in #1740: generate every unit as a
UnitOf<TQuantity>, such asLengthUnits.Meter, next to the unit enums. Opened as a draft until the direction in #1740 is agreed.LengthUnits), with astatic readonly UnitOf<Length>field per unit holding its names, base units and exact conversions.UnitOfitself knows nothing about enums orUnitKey.new (LengthUnit.Foot, LengthUnits.Foot), so each unit's data is generated in one place.As/Fromlook built-in units up in the quantity'sQuantityInfo, so they convert exactly like their enum values, configured conversions included, andFromkeeps the quantity in that unit. Custom units still use their own conversions.UnitOf<TQuantity>derives from a new non-genericUnitOf. It holds the data, so the code isn't compiled again for each quantity. It's also the type a futureIQuantity.Unitwould return.Purely additive. It builds on #1737 and #1738, which should be merged first.
ToString(), cold processAgainst #1737 alone, the generated units cost about 59 KB, plus about 13 ms and 436 KB of allocations when every quantity is created.
Part of #1740.
🤖 Generated with Claude Code
https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG