You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"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:
DistancewheelDiameter = Unit.Inches(6); // Unit would contain all valid types of unitsDistancewheelDiameter = 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:
varwheelDiameter = Unit.Inches(6); // wheelDiameter will be of the type DistancevarkMaxSpeed = Unit.MetersPerSecond(2.5); // wheelDiameter will be of the type LinearVelocity
The text was updated successfully, but these errors were encountered:
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.
Uh oh!
There was an error while loading. Please reload this page.
The current way to create a distance in inches (from https://docs.wpilib.org/en/latest/docs/software/basic-programming/java-units.html):
"Inches of 6" doesn't read naturally, making this code a bit awkward. Changing
of
tofrom
was suggested, but although this is slightly better, it's still a bit weird.Something like one of these would feel more natural:
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:The text was updated successfully, but these errors were encountered: