Skip to content

💥Require unit enum types to also be struct #1472

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

Merged
merged 2 commits into from
Dec 29, 2024

Conversation

angularsen
Copy link
Owner

@angularsen angularsen commented Dec 26, 2024

Change all where TUnitType : Enum generic type constraints to include struct constraint, to avoid usages like this.

UnitsNetSetup.Default.UnitParser.Parse<LengthUnit>("abc"); // Ok
UnitsNetSetup.Default.UnitParser.Parse<Enum>("abc"); // Compiles, but throws exception

This also fixes inconsistency, between UnitAbbreviationsCache.GetUnitAbbreviations<TUnitType> and UnitAbbreviationsCache.GetDefaultAbbreviation<TUnitType>.

Changes

  • Add struct constraint to all Enum constraints
  • Remove two test cases MapAndLookup_MapWithSpecificEnumType_LookupWithEnumType, MapAndLookup_WithEnumType

Noteworthy

There are some minor use cases for passing non-generic Enum value, like getting unit abbreviations for an IQuantity such as UnitAbbreviations.GetDefaultAbbreviation(quantity.Unit).
After this PR, you now have to do GetDefaultAbbreviation(quantity.Unit.GetType(), Convert.ToInt32(quantity.Unit)) instead.
GetAbbreviations() already had this requirement, so now it's more consistent at least.

Sources

https://stackoverflow.com/a/74773273
https://devblogs.microsoft.com/premier-developer/dissecting-new-generics-constraints-in-c-7-3/

Change all `where TUnitType : Enum` generic type constraints to include `struct` constraint, to avoid usages like this.

```cs
UnitsNetSetup.Default.UnitParser.Parse<LengthUnit>("abc"); // Ok
UnitsNetSetup.Default.UnitParser.Parse<Enum>("abc"); // Compiles, but throws exception
```

This also fixes inconsistency, between `UnitAbbreviationsCache.GetUnitAbbreviations<TUnitType>` and `UnitAbbreviationsCache.GetDefaultAbbreviation<TUnitType>`.

### Changes
- Add `struct` constraint to all `Enum` constraints
- Remove two test cases `MapAndLookup_MapWithSpecificEnumType_LookupWithEnumType`, `MapAndLookup_WithEnumType`

### Noteworthy

There are some minor use cases for passing non-generic `Enum` value, like getting unit abbreviations given a `IQuantity` such as `UnitAbbreviations.GetDefaultAbbreviation(quantity.Unit)`. Now this requires using `GetDefaultAbbreviation(quantity.Unit.GetType(), Convert.ToInt32(quantity.Unit))` instead. However, `GetAbbreviations()` already had this requirement, so now it's more consistent.

### Sources
https://stackoverflow.com/a/74773273
https://devblogs.microsoft.com/premier-developer/dissecting-new-generics-constraints-in-c-7-3/
@angularsen angularsen requested a review from lipchev December 26, 2024 23:08
Copy link

codecov bot commented Dec 26, 2024

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 86%. Comparing base (8b3696b) to head (28a2613).
Report is 1 commits behind head on release/v6.

Files with missing lines Patch % Lines
UnitsNet/QuantityDisplay.cs 0% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           release/v6   #1472   +/-   ##
==========================================
  Coverage          86%     86%           
==========================================
  Files             298     298           
  Lines           30515   30515           
==========================================
  Hits            26405   26405           
  Misses           4110    4110           

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

Comment on lines -218 to 221
public string[] GetUnitAbbreviations<TUnitType>(TUnitType unit, IFormatProvider? formatProvider = null) where TUnitType : Enum
public string[] GetUnitAbbreviations<TUnitType>(TUnitType unit, IFormatProvider? formatProvider = null) where TUnitType : struct, Enum
{
return GetUnitAbbreviations(typeof(TUnitType), Convert.ToInt32(unit), formatProvider);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is great, introducing the struct constraint here opens up the possibility for using
Unsafe.As<TUnit, int>(ref unit)
instead of
Convert.ToInt32(unit)
which is ~34x faster on net8.0 and ~92x faster on net48 🚀

Copy link
Owner Author

Choose a reason for hiding this comment

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

Sounds good!

Copy link
Collaborator

Choose a reason for hiding this comment

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

This one is really bugging me with it's 0% coverage - I'm definitely going to do something about it soon..

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we want to be really strict about it, we should consider making the QuantityInfo class abstract- this way we know that the user won't be able to implement IQuantity without having actually implemented IQuantity<TUnit>.

Copy link
Collaborator

@lipchev lipchev left a comment

Choose a reason for hiding this comment

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

This all looks great. Just one question - do you have a preference with the way that the generic constraints are positioned (same line or new line)?

@angularsen
Copy link
Owner Author

This all looks great. Just one question - do you have a preference with the way that the generic constraints are positioned (same line or new line)?

Great! No, I'm too old to care about these things anymore 😅 Yolo!
As long as it's follows most typical c# norms, I'm happy.

…traint

# Conflicts:
#	UnitsNet.Tests/UnitAbbreviationsCacheTests.cs
@angularsen angularsen merged commit 3f1456d into release/v6 Dec 29, 2024
1 check was pending
@angularsen angularsen deleted the agl/struct-constraint branch December 29, 2024 01:19
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.

2 participants