Skip to content

[v5] Convert quantities to and from a UnitOf<TQuantity> - #1739

Open
tmilnthorp wants to merge 1 commit into
angularsen:maintenance/v5from
tmilnthorp:tmilntho/unitof-v5
Open

tmilnthorp wants to merge 1 commit into
angularsen:maintenance/v5from
tmilnthorp:tmilntho/unitof-v5

Conversation

@tmilnthorp

Copy link
Copy Markdown
Collaborator

Backport of the forward-compatible part of #1738 to v5. It adds UnitOf<TQuantity>, a unit of a quantity described by its names and its value in the base unit, without a unit enum value. Quantities can be converted to and from such a unit with no setup.

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

double furlongs = Length.FromMiles(1).As(furlong); // 8
Length length = Length.Info.From(2, furlong);      // 402.336 m, in the base unit
  • UnitOf<TQuantity> optionally 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. Using a unit of another quantity doesn't compile.
  • quantityInfo.From(value, UnitOf<TQuantity>) returns the quantity in its base unit. It throws ArgumentException for the info of another quantity.

Purely additive, and the same call sites compile on v6, so code written against this keeps working after upgrading. v6 builds further on it:

  • exact fraction values;
  • units with an offset, such as temperatures;
  • IUnitDefinition;
  • compile-time checking for From;
  • the groundwork for units UnitsNet doesn't define to be used like built-in ones, which is v6-only.

Related: #1738, #1246, #1562

🤖 Generated with Claude Code

https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG

UnitOf<TQuantity> describes a unit of a quantity by its names and its value in
the base unit of the quantity, 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.

This is the subset of the v6 API that v5 supports, so code written against it
keeps compiling on v6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012XKhDsyHDc5BHrmibxScqG

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