Std.Measurements
View source →Literal units of measure (design 2026-07-08-units-macro-design).
A number written immediately before a registered suffix expands, at parse
time, into the matching constructor call — no runtime cost beyond the
wrapped integer. The suffixes are globally active (like :syntax macros),
so use Std.Measurements is only needed to bring the operations and the wrapper
types into scope, not to enable the sugar.
500ms ==> Std.Measurements.ms(500) # a Duration of 500_000 microseconds 3khz ==> Std.Measurements.khz(3) # a Frequency of 3_000 hertz 80pct ==> Std.Measurements.pct(80) # a Percent 9600baud ==> Std.Measurements.baud(9600) # a Baud rate
Each quantity is a closed record over one Int, so arithmetic stays
same-unit: add/sub/scale are defined per quantity and there is no
cross-quantity operation, which is what makes 500ms and 3khz unmixable.
Example
use Std.Measurements
let budget = add(500ms, 250ms) # a Duration of 750_000 microseconds
as_ms(budget) # => 750
A length of time, stored as microseconds.
Functions
-
# fn add(a: Duration, b: Duration) -> Duration
Sum of two Durations.
-
# fn as_ms(d: Duration) -> Int
The Duration truncated to whole milliseconds.
-
# fn as_s(d: Duration) -> Int
The Duration truncated to whole seconds.
-
# fn as_us(d: Duration) -> Int
The Duration as a whole number of microseconds.
-
# fn baud(n: Int) -> Baud
A Baud rate of
nsymbols per second. -
# fn hz(n: Int) -> Frequency
A Frequency of
nhertz. -
# fn khz(n: Int) -> Frequency
A Frequency of
nkilohertz. -
# fn ms(n: Int) -> Duration
A Duration of
nmilliseconds. -
# fn pct(n: Int) -> Percent
A Percent of
n. -
# fn s(n: Int) -> Duration
A Duration of
nseconds. -
# fn scale(d: Duration, k: Int) -> Duration
A Duration scaled by an integer factor.
-
# fn sub(a: Duration, b: Duration) -> Duration
Difference of two Durations (
a - b). -
# fn us(n: Int) -> Duration
A Duration of
nmicroseconds.