wpilibsuite / allwpilib

Official Repository of WPILibJ and WPILibC
https://wpilib.org/
Other
1.04k stars 605 forks source link

[wpiunit] Add Measure.divide(Measure<U2>) #6611

Closed WispySparks closed 2 months ago

WispySparks commented 2 months ago

Adds a divide method to measure that performs dimensional analysis similar to the times method. Use case would be something like Meters.of(3).divide(MetersPerSecond.of(3) which comes out to one second. I also added tests for it and consolidated the old divide(Measure) into the new divide method

WispySparks commented 2 months ago

Not sure if suppressing the warnings in EncoderTest or changing ticksPerRevolution to a double is preferred, unless there's a third option I'm missing

SamCarlberg commented 2 months ago

Ah, type erasure bites again. The reason why divide only supported dimensionless values was to be able to return a measure of a known unit in the method signature. Changing it to accept any unit type means it must return a wildcard measure. Then you either need to do that unchecked cast to a known unit type or do a workaround without having complete type safety (eg making ticksPerRevolution a raw double instead of a unit safe ratio).

Unfortunately there's no good solution here with the way the library is currently set up. Casting works if you know what the dimensional analysis will return; using a raw double removes some of the unit safety we want to encourage people to use. For the tests, either way will be acceptable.