Skip to content
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

🛡️Restrict number extension methods to INumber<T> #1281

Merged
merged 3 commits into from
Jul 12, 2023

Conversation

angularsen
Copy link
Owner

@angularsen angularsen commented Jul 10, 2023

Fixes #1269

Quantities implement IConvertible.ToDouble() by converting the Value property to double, and similar for other number types.

Number extension methods expects a number argument, but take any argument type and call Convert.ToDouble() on it. This means you can run into subtle bugs:

Length.FromCentimeters(100).Millimeters(); // Length of 100 mm
Length.FromCentimeters(100).Millimeters; // Value of 1000

The quantity implements conversions like this:

        double IConvertible.ToDouble(IFormatProvider? provider)
        {
            return Convert.ToDouble(_value);
        }

The extension method looks like this:

public static Length Millimeters<T>(this T value)
{
	return Length.FromMillimeters(Convert.ToDouble(value));
}

So Convert.ToDouble() happily converts Length.FromCentimeters(100) to 100.

Changes

  • Restrict number extension methods to INumber<T> for net7.0 and later

@codecov
Copy link

codecov bot commented Jul 12, 2023

Codecov Report

Patch coverage: 100% and no project coverage change.

Comparison is base (9e826e3) 84% compared to head (60a9c98) 84%.

Additional details and impacted files
@@          Coverage Diff           @@
##           master   #1281   +/-   ##
======================================
  Coverage      84%     84%           
======================================
  Files         335     335           
  Lines       31837   31837           
======================================
  Hits        26821   26821           
  Misses       5016    5016           
Impacted Files Coverage Δ
...erToAbsorbedDoseOfIonizingRadiationExtensions.g.cs 100% <100%> (ø)
.../GeneratedCode/NumberToAccelerationExtensions.g.cs 100% <100%> (ø)
...ratedCode/NumberToAmountOfSubstanceExtensions.g.cs 100% <100%> (ø)
...eneratedCode/NumberToAmplitudeRatioExtensions.g.cs 100% <100%> (ø)
...ensions/GeneratedCode/NumberToAngleExtensions.g.cs 100% <100%> (ø)
...eneratedCode/NumberToApparentEnergyExtensions.g.cs 100% <100%> (ø)
...GeneratedCode/NumberToApparentPowerExtensions.g.cs 100% <100%> (ø)
...s/GeneratedCode/NumberToAreaDensityExtensions.g.cs 100% <100%> (ø)
...tensions/GeneratedCode/NumberToAreaExtensions.g.cs 100% <100%> (ø)
...tedCode/NumberToAreaMomentOfInertiaExtensions.g.cs 100% <100%> (ø)
... and 110 more

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@angularsen angularsen merged commit d1cc5f8 into master Jul 12, 2023
3 checks passed
@angularsen angularsen deleted the agl/numex-inumber branch July 12, 2023 12:25
@angularsen
Copy link
Owner Author

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.

None yet

1 participant