Duration Literals
The Duration and Instant types have specialized literal support.
Syntax#
Duration literals are written as a decimal integer or decimal float immediately followed by a unit suffix:
- Examples:
10ns250us5ms1s1.5s2min1h
The unit suffix is part of the literal token; the lexer must not split it into an integer token followed by an identifier.
Units#
Recognized suffixes:
ns— nanosecondsus— microsecondsms— millisecondss— secondsmin— minutesh— hoursd— days
Semantics#
Duration literals evaluate to a Duration value represented as an i64
nanosecond count.
- For integer forms (e.g.
5ms), the value is scaled exactly. - For floating-point forms (e.g.
1.5s), the value is scaled and then rounded toward zero to an integral nanosecond count.
If the scaled value does not fit in i64, compilation fails.
Compiler requirements:
- Implement lexing rules that distinguish unit suffixes from identifiers.
- Map duration literals to the
Durationtype with correct unit scaling. - Ensure constant-evaluation behavior (rounding, overflow) matches the spec.
Source repository · Edit this page · View Markdown