Let's say I'd like to ensure type safety in Java on primitive types. For the sake of an example, let us want to distinguish a Ratio
from an AbsoluteValue
, both are represented by a double
.
Java to my best knowledge does not support type synonyms.
So I'm quite confident some overhead will be involved.
From the top of my head I could think of either defining new calsses which wrap double
(a lot of boilerplate), or subclassing Double
... or maybe there are even other options?
Which approach gives me the least performance overhead?
EDIT: I'm confined to Java 8, so that's the implicit basis of the question; however, if there are alternative solutions in newer versions of the language I'd still be keen to hear about them.
@Ratio double
is not assignable from@AbsoluteValue double
. I don’t know whether there are ready-to-use solutions, like configuring existing checker tools to add that rule. Treatingint
likeenum
is a related example of using a primitive type with semantics. – Ursal