(ThermodynamicTemperature × ThermodynamicTemperature).sqrt returns TemperatureInterval
taking a .sqrt() after multiplying two Lengths, the dimensions behave as expected.
let d1: Length = Length::new::<uom::si::length::meter>(20.);
let d2: Length = Length::new::<uom::si::length::meter>(20.);
let d3: Length = (d1 * d2).sqrt(); // This makes sense.
Trying to do the same for ThermodynamicTemperature, there appears to be something strange with the return type.
let t1: ThermodynamicTemperature = ThermodynamicTemperature::new::<kelvin>(273.15);
let t2: ThermodynamicTemperature = ThermodynamicTemperature::new::<kelvin>(400.);
//let t3: ThermodynamicTemperature = (t1 * t2).sqrt(); // Error: Can't explicitly state type here
let t3 = (t1 * t2).sqrt(); // It does compile when you don't explicitly state a type.
let t4: ThermodynamicTemperature = t3 + t2; // Adding the implicitly typed t3 with t2 creates a valid result
It's strange that adding the implicitly typed result still adds with the explicitly typed t2, but that the types are not actually the same to the compiler. I can't make much sense of the actual compiler diagnostic either;
error[E0308]: mismatched types
--> src/diffusion_coefficient.rs:57:40
|
57 | let t3: ThermodynamicTemperature = (t1 * t2).sqrt(); // Error: Can't enforce type here
| ------------------------ ^^^^^^^^^^^^^^^^ expected trait `TemperatureKind`, found trait `Kind`
| |
| expected due to this
|
= note: expected struct `Quantity<dyn Dimension<I = Z0, J = Z0, Kind = ..., L = ..., M = ..., N = ..., T = ..., Th = ...>, ..., ...>`
found struct `Quantity<dyn Dimension<I = Z0, J = Z0, Kind = dyn Kind, L = Z0, M = Z0, N = Z0, T = Z0, Th = ...>, ..., ...>`
I got in the habit of explicity typing variables, as it makes the code significantly more legible when using sub-optimal inlay type hints. In this case, this does not appear to be possible. Workaround is of course to just let it be implicitly typed, but being able to type this explicitly would be even better!
Upon further investigation, it appears that the .sqrt() operation returns a TemperatureInterval instead. I assume this is intentional behaviour?
let t1: ThermodynamicTemperature = ThermodynamicTemperature::new::<kelvin>(273.15);
let t2: ThermodynamicTemperature = ThermodynamicTemperature::new::<kelvin>(400.);
let t3: TemperatureInterval = (t1 * t2).sqrt(); // Different from what I expected, would expect ThermodynamicTemperature.
See thermodynamic temperature for a starter. What's happening is that is that once an operation is performed on the original thermodynamic temperature to get a new quantity the result is given the default Kind marker trait. As a work-around you can do the following to turn your temperature interval back into a thermodynamic temperature:
let t0: ThermodynamicTemperature = ThermodynamicTemperature::new::<kelvin>(0.0);
let t1: ThermodynamicTemperature = ThermodynamicTemperature::new::<kelvin>(273.15);
let t2: ThermodynamicTemperature = ThermodynamicTemperature::new::<kelvin>(400.);
let t3: ThermodynamicTemperature = t0 + (t1 * t2).sqrt();