Home / Docs / Diagnostics
U2028: Values of different units meet, or a measured and a unitless value
Values of different units meet, or a measured value meets a unitless one.
Why Una rejects it
In edition 0.3, a unit is part of a number type (ADR 0104, ADR 0114 rule 12, ADR 0120 rules 61 to 65): Int of Meter is neither Int nor Int of Second, and F64 of Meter is neither F64 nor F64 of Second. These agree on their unit:
- the operands of
+,-,modand of a comparison; - an argument and its parameter;
- a result and its declared type.
A value changes unit only through convert(), which multiplies by an exact integer ratio between units of one dimension, or leaves or takes a unit through magnitude() and measured().
The host of a drive section (edition 0.4 preview, spec/26 §2 rule 2) hands the state of the driven module from init to step and from init and step to on_stop unchanged, so that hand-off is such a flow too. The units of the result type of init and of the state parameters of step and on_stop MUST be the same at every position, at the top and in each type argument, as Option[Int of Second]. init() -> Int of Second with step(edit state: Int of Millisecond, input: Tick) is U2028 at the type of state. una run and una build refuse such a module too.
A unit with no base unit and a scale other than 1, as Kilometer / Meter, stays a unit, and its magnitude() counts in steps of that scale.
The repair at a glance
module demo
pub measure Meter
pub measure Kilometer = 1000 * Meter
- pub total(a: Int of Meter, b: Int of Kilometer) -> Int of Meter = a + b
+ pub total(a: Int of Meter, b: Int of Kilometer) -> Int of Meter = a + b.convert()Failing example
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"module demo
pub measure Meter
pub measure Kilometer = 1000 * Meter
pub total(a: Int of Meter, b: Int of Kilometer) -> Int of Meter = a + bFixed example
[package]
name = "demo"
version = "0.1.0"
edition = "0.3"module demo
pub measure Meter
pub measure Kilometer = 1000 * Meter
pub total(a: Int of Meter, b: Int of Kilometer) -> Int of Meter = a + b.convert()