Skip to content

Compound Dimension types in Java Units Library? #6362

Answered by SamCarlberg
chauser asked this question in General
Discussion options

You must be logged in to vote

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 (eg Velocity<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 type Measure with no type arguments, which is at odds with a type-safe unit system.

The poor ergon…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@chauser
Comment options

@SamCarlberg
Comment options

Answer selected by chauser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants