Repository navigation
Generate units as UnitOf<TQuantity>, such as LengthUnits.Meter - #1741
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:
|
|
I like the typed-unit direction. I’d like to work through how an application can register one of these definitions and retain it on a quantity. Two furlong examples illustrate the difference. One-off conversion with the proposed API. var furlong = new UnitOf<Length>("Furlong", "Furlongs", 201.168m);
var converted = Length.Info.From(2, furlong); // 402.336 m, stored as metersA unit registered in an application’s setup today. This retains the selected unit and gives the parser, formatter, and converter the same metadata. The application allocates an enum value for it. const LengthUnit furlongSlot = (LengthUnit)1000;
var culture = CultureInfo.GetCultureInfo("en-US");
var info = Length.LengthInfo.CreateDefault(units => units.Append(
new UnitDefinition<LengthUnit>(
furlongSlot, "Furlong", "Furlongs", BaseUnits.Undefined,
conversionFromBase: QuantityValue.FromTerms(1000, 201168))));
var setup = UnitsNetSetup.Create(b => b.WithQuantities([info]));
setup.UnitAbbreviations.MapUnitToDefaultAbbreviation(furlongSlot, culture, "fur");
var retained = setup.QuantityParser.Parse<Length, LengthUnit>(
"2 fur", culture, Length.From);
var text = setup.Formatter.Format(retained, "G", culture); // 2 furAn external app can construct There’s a built-in case worth testing. After configuring one foot to equal two meters, converting two meters through As part of replacing the enums, how do you see What identity should a registered unit carry so the parser, formatter, converter, and deserializer can all resolve it, including in a fresh process? A stable key scoped by quantity and definition owner is one option. How would the migration keep existing enum names and numeric values readable? Migration proposal |
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