Repository navigation
Convert quantities to and from a UnitOf<TQuantity> - #1738
Open
tmilnthorp wants to merge 4 commits into
Open
tmilnthorp wants to merge 4 commits into
tmilnthorp wants to merge 4 commits into
Conversation
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
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1738 +/- ##
======================================
Coverage 98% 98%
======================================
Files 515 516 +1
Lines 24092 24115 +23
======================================
+ Hits 23692 23715 +23
Misses 400 400
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 was referenced Oct 6, 2026
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.
Adds
UnitOf<TQuantity>, a unit of a quantity described by its names and conversions, without a unit enum value. Quantities can be converted to and from such a unit with no setup, and since the unit is tied to its quantity, using it with another quantity doesn't compile.UnitOf<TQuantity>implementsIUnitDefinition. It's created from the value of one unit in the base unit, or from conversion expressions for units with an offset, such as temperatures. Optionally, it takes the base units it's made of, e.g. feet per day:new BaseUnits(length: LengthUnit.Foot, time: DurationUnit.Day).quantity.As(UnitOf<TQuantity>)returns the value in that unit.QuantityInfo<TQuantity, TUnit>.From(value, UnitOf<TQuantity>)returns the quantity in its base unit, since a quantity can still only be stored in one of its own units.This is purely additive; no existing API changes. It's a first step toward units that UnitsNet doesn't define being usable like built-in ones. Parsing, formatting and storing a quantity in such a unit aren't covered yet. The subset that v5 supports is backported in #1739, so code written against it keeps compiling on v6.
Related: #1246, #1562, #1200
🤖 Generated with Claude Code
https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG