-
Notifications
You must be signed in to change notification settings - Fork 614
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
Java "Unit" classes #5347
Comments
The reason we haven't done this throughout the entire library is the memory pressure. Unlike C++, any time you use a unit in Java, you'd have a memory allocation. The library is already up against a lot of memory and CPU pressure, especially on the roboRIO 1, and the effects units would have on those would likely cause major issues for teams. Java does not have an allocation free solution for units currently. |
It would be nice if we could find some alternative, even if it's subpar, that doesn't use up memory allocation. Even an improved way to denote what units need to be passed into a function would be nice. Only way I could think of doing that would be an equivalent to Alternatively: the classes could still be used, but their values wouldn't get passed into any WPILib classes; instead, they'd exist, and you'd put e.g. |
Is that different from https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/util/Units.java? |
Yeah. Using the |
That still requires allocations. Which are the problem. |
If the allocations are done solely by the user, they have control over how many are done. This shouldn't be done within WPILib itself. If it makes more sense to have it then only be a user-defined class then that's probably fine. |
If the interface requires a user make an allocation (create an object) to call the function, then it’s not a choice. The only way that the number of allocations can be controlled by the user with such an interface is if the objects are mutable. Mutable numeric objects are generally a poor design practice since Java passes objects by reference and thus it becomes very easy to accidentally mutate what is logically a fixed value. |
Is your feature request related to a problem? Please describe.
The lack of sane units in Java can cause hard-to-identify problems where wrong units are passed into the wrong place. These are sneaky and hard to debug.
Describe the solution you'd like
Sane unit classes that represent some kind of physical value, for example, a
Distance
class for meters, feet, etc. that can take in anything (e.g.Distance.fromFeet
and<Distance>.asInches
). Same for velocity, angular velocity, acceleration, etc.Describe alternatives you've considered
Implementing these in our own team library: https://github.com/Team4028/2023-ChargedUp/tree/develop/src/main/java/frc/lib/beaklib/units
Although it worked, there is much to be desired and having wider, library-wide support would be extremely helpful.
Furthermore, we tried comments to denote units in places that didn't take in our own unit classes. However, this was clunky, easy to forget, and easy to ignore.
Additional context
An example of what I'd like is seen in our team library: https://github.com/Team4028/2023-ChargedUp/tree/develop/src/main/java/frc/lib/beaklib/units
I have no idea if this is really feasible but it would be extremely helpful.
The text was updated successfully, but these errors were encountered: