Since the parallel is drawn to C++, I’d like to attract attention on the fact that to avoid conflict between standard literal suffixes and user-defined literal suffixes, the C++ Standard mandates that user-defined literal suffixes must begin by _.
Since similar issues could occur in Rust, it might be worth applying the same rule:
use std::time::duration_literals;
let dt1: Duration = 1_s + 200_ms; // or just 1.2s, see further
let dt2: Duration = 10_us;
let dt3: Duration = 10.2_µs; // µs and us are equivalent, proposal can support both
use simple_units::literals::*;
let distance1: Meter = 1_m;
let distance2: Meter = 5_nm;
let accel1: MeterPerSecond2 = 2.5_g;
let accel2: MeterPerSecond2 = 3[m/s^2]; // see further on how square brackets work
So, the difference really is 1_s vs 1.s().
Disclaimer: the following is a shoot in the dark; it is unclear to me whether the proposed syntax could conceivably cause ambiguities. I’d expect not, since methods are available on primitives and data-members are available on expressions, but I have not proved it.
The noisy () could potentially be reduced by considering the addition of Properties to the language. Specifically, by making it so that 1.s evaluates to 1.s() when s is a property-able method.
This would let us rewrite the example above as:
use std::time::TimeProperties;
let dt1: Duration = 1.s + 200.ms; // or just 1.2s, see further
let dt2: Duration = 10.us;
let dt3: Duration = 10.2.µs; // µs and us are equivalent, proposal can support both
use si::DistanceProperties;
let distance1: Meter = 1.m;
let distance2: Meter = 5.nm;
let accel1: MeterPerSecond2 = 2.5.g;
let accel2: MeterPerSecond2 = 3.m / (1.s * 1.s); // not the greatest, I'll admit.
This does not support compound units, however should be familiar to most users:
- It uses method calls on primitives, which is widely available, either through monkey-patching or method extensions.
- It uses properties, which is widely available.