Skip to content

Units: Measures are a bit weird to create #7945

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

Open
qwertychouskie opened this issue Apr 29, 2025 · 2 comments
Open

Units: Measures are a bit weird to create #7945

qwertychouskie opened this issue Apr 29, 2025 · 2 comments
Assignees

Comments

@qwertychouskie
Copy link

qwertychouskie commented Apr 29, 2025

The current way to create a distance in inches (from https://docs.wpilib.org/en/latest/docs/software/basic-programming/java-units.html):

Distance wheelDiameter = Inches.of(6);

"Inches of 6" doesn't read naturally, making this code a bit awkward. Changing of to from was suggested, but although this is slightly better, it's still a bit weird.

Something like one of these would feel more natural:

Distance wheelDiameter = Unit.Inches(6);     // Unit would contain all valid types of units
Distance wheelDiameter = Distance.Inches(6); // Distance would contain valid types of distance units

This way, you can also use var, since the created type will be pretty clear, and you won't have to remember which exact class contains the unit you want:

var wheelDiameter = Unit.Inches(6);        // wheelDiameter will be of the type Distance
var kMaxSpeed = Unit.MetersPerSecond(2.5); // wheelDiameter will be of the type LinearVelocity
@PeterJohnson
Copy link
Member

The proposed approach isn’t naturally extensible to user-created units though, as users can’t inject new unit static functions into the Units class.

@qwertychouskie
Copy link
Author

The proposed approach isn’t naturally extensible to user-created units though, as users can’t inject new unit static functions into the Units class.

You could create a MyUnits class that extends Unit and implements your custom units. Alternatively, you could just use the old syntax anywhere you create custom units, there's no reason that the approaches can't co-exist. That said, in what circumstances would custom units be needed in the first place? There are only so many units that exist in the world, unless I'm missing something.

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

No branches or pull requests

3 participants