Skip to content

Add type safety and improve type inference in UnitsMath.cs #1373

@UrielZyx

Description

@UrielZyx

Is your feature request related to a problem? Please describe.
Some of the extension methods inUnitsMath.cs (e.g. Average) take an Enum unitType argument. Our system uses these extensively, and when copying/pasting code (and changing the quantity type because we're dealing with different sensors every time), I'd like the compiler to remind me I forgot to update the unit.

Describe the solution you'd like
Change

public static TQuantity Average<TQuantity>(
    this IEnumerable<TQuantity> source,
    Enum unitType)
    where TQuantity : IQuantity

into something like:

public static TQuantity Average<TQuantity>(
    this IEnumerable<TQuantity> source,
    TUnitType unitType)
    where TUnitType : Enum
    where TQuantity : IQuantity<TUnitType>

Describe alternatives you've considered
We tried implementing type safe methods in our own codebase, but when we use the wrong unit type, our IDE just falls back to the implementation in UnitsMath.

Additional context

  • This will break backwards compatibility, but only for people doing really weird things.
  • This change might be warranted in a lot of other places that take a unitType parameter (I haven't checked yet, mostly because they didn't come up in my use-case)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions