Compound Dimension types in Java Units Library? #6362
-
In working with arm torque calculations for our arm I'm really wishing I had a way to name compound dimensions such as Force and Torque instead of writing
and
every time torque or force is needed as part of an even larger type. Is there any hope that this can be done, either by the user or as part of the library? The limitation noted in the docs that Also even if there isn't a way to do this, having Newton and PoundForce units with type This might be considered a feature request, but it feels more like discussion at this stage. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Since Java doesn't support typedefs, I'm not sure this can be done. |
Beta Was this translation helpful? Give feedback.
It was a design decision. There's a trade off between type safety, unit safety, and API shape. The
Unit
class doesn't need to have the self type as a generic type parameter, and it would actually be possible to inherit from other unit types (egVelocity<D> extends Per<D, Time>
) if we didn't have that constraint, but it allows us to enforce a type safe contract on subclasses ("a unit type U must provide a method that accepts a magnitude and returns a Measure", for instance). That would not be possible without knowing the self type; the best we could do is have the base Unit class return a raw typeMeasure
with no type arguments, which is at odds with a type-safe unit system.The poor ergon…