Skip to content

Replace ToUnit(UnitSystem) from the IQuantity<TUnit> interface with an extension method #1606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

lipchev
Copy link
Collaborator

@lipchev lipchev commented Aug 16, 2025

  • replaced the ToUnit(UnitSystem) overload from the IQuantity<TUnit> interface with an extension method ([Obsolete])
  • renamed the ToUnitUntyped extension back to ToUnit and made it [Obsolete]
  • replaced the generated UnitSystem tests for the IQuantity / IQuantity<TUnit> with a single set of tests in the IQauntityTests

lipchev and others added 7 commits August 9, 2025 22:51
…tensions

- removed the As(UnitSystem) method from the IQuantity interface
Split tests into ToUnit vs ToUnitTyped
Update calls to ToUnitTyped where output is IQuantity.
…interface with an extension method ([Obsolete])

- renamed the ToUnitUntyped extension back to ToUnit and made it [Obsolete]
- replaced the generated UnitSystem tests for the IQuantity / IQuantity<TUnit> with a single set of tests in the IQauntityTests
@lipchev
Copy link
Collaborator Author

lipchev commented Aug 16, 2025

@angularsen This corresponds to option 2) from this comment: #1600 (comment)

Comment on lines 413 to -432
public void ToUnit_UnitSystem_ReturnsValueInDimensionlessUnit()
{{
Assert.Multiple(() =>
{{
var quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});

{_quantity.Name} convertedQuantity = quantity.ToUnit(UnitSystem.SI);

Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
Assert.Equal(quantity.Value, convertedQuantity.Value);
}}, () =>
{{
IQuantity<{_unitEnumName}> quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});

IQuantity<{_unitEnumName}> convertedQuantity = quantity.ToUnit(UnitSystem.SI);

Assert.Equal({_baseUnitFullName}, convertedQuantity.Unit);
Assert.Equal(quantity.Value, convertedQuantity.Value);
}});
}}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only kept the concrete part of this test, but on second thought there shouldn't really be any need for it to exist here either- as we already have tests for the UnitSystem constructor.
In case we decide to move forward with the removal of the interface methods from the interface in v6, then these tests should probably go to another test file- something like QuantityExtensionsTests.

@lipchev
Copy link
Collaborator Author

lipchev commented Aug 19, 2025

@angularsen This corresponds to option 2) from this comment: #1600 (comment)

One point I forgot to mention in the other thread was the requirement for using UnitsNet. This isn't a big deal most of the time, and importing the namespaces from the IDE isn't very difficult, however if we wanted to minimize these cases, we could potentially have the unit conversion extensions, of which we currently only support the ones with UnitSystem (but you know the As(TUnit) and ToUnit(TUnit) are coming in the 🐲 ), moved to the UnitsNet.Units namespace, where the actual units are defined. This is why in the 🐲 PR I used to have these extensions placed in the UnitConversionExtensions class (until last night, when I forgot about the whole namespace-trick-thing and merged them back into the QuantityExtensions).

So, there you have it, another thing for you to consider (I can't say I have any preferences- one way or the other).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants