I have hit a problem while working with the Units of Measurements functionality in metascala, defined in the file Units.scala.
For the remainder of this question, I will use a simplified scheme, with only one Unit type, length.
So where in reality a type looks like
Quantity[_1, _0, _0, _0, _0, _0, _0]
^ ^ ^ ^ ^ ^ ^
| | | | | | |
| Mass | Crncy.| Mol |
Length Time Temp. Lum.Intensity
this will be sufficient for demonstrating the problem:
Quantity[_1]
^
|
Length
As soon as the type needs to be inferred, the trouble starts.
Consider this example (also have a look at the code from UnitsTest.scala):
val length: Quantity[_1] = m(5)
val area: Quantity[_2] = length * length // (1) Works
val dist: Quantity[_1] = area / length // (2) Doesn't work!
I get an error in the last line saying:
type mismatch;
found :
scalax.units.Units.Quantity[
scalax.units.Subtractables.-[
scalax.units.Integers._2,
scalax.units.Integers._1
]
]
required:
scalax.units.Units.Quantity[
scalax.units.Integers._1
]
It looks like the compiler can’t figure out that the type at hand is equal to Quantity[_1] when “substracting a dimension”, e. g. going from area to dist like in (1):
Quantity[_2 - _1] <<not equal to>> Quantity[_1]
The confusing thing is that it works when “adding a dimension” e. g. going from length to area like in (2):
Quantity[_1 + _1] <<equal to>> Quantity[_2]
(Sorry for not pasting the whole code here, it is just too much. I tried to minimize my example, but I failed. That’s why I’m just linking to it.)
Type
SubfromSubtractableis missing in theMInttrait. A simple definition to make it work would be to perform a negative addition when you want to subtract a type inMSuccandMNeg.One more thing, the
/method inQuantityshould divide its parameters and not multiply them!