Skip to content

Convert quantities to and from a UnitOf<TQuantity> - #1738

Open
tmilnthorp wants to merge 4 commits into
angularsen:masterfrom
tmilnthorp:tmilntho/unit-definition-conversions
Open

tmilnthorp wants to merge 4 commits into
angularsen:masterfrom
tmilnthorp:tmilntho/unit-definition-conversions

Conversation

@tmilnthorp

@tmilnthorp tmilnthorp commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

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.

var furlong = new UnitOf<Length>("Furlong", "Furlongs", 201.168); // 1 furlong = 201.168 m

Length.FromMiles(1).As(furlong);     // 8
Length.Info.From(2, furlong);        // 402.336 m
Mass.FromKilograms(1).As(furlong);   // doesn't compile
  • UnitOf<TQuantity> implements IUnitDefinition. 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

tmilnthorp and others added 4 commits October 6, 2026 14:06
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

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98%. Comparing base (3bf1ef4) to head (f3eaa8f).

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           
Flag Coverage Δ
net48 97% <100%> (+<1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant