WideningNumeric
Requires the compiler's strict implicit numeric widening from Source to Self.
Module std.math
import std.math;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
WideningNumeric
pub concept WideningNumeric<Self, Source>Requires the compiler's strict implicit numeric widening from Source to Self.
Only canonical standard-package identity grants this compiler predicate; a same-named user concept keeps ordinary structural meaning. Exact equal types, equal-width different primitives, byte, mixed signedness and incompatible integer/floating domains fail. Target-width facts come from the selected target, not the host. No runtime witness, allocation or callable is introduced. Self: Strictly wider ordinary numeric destination representation. Source: Exact ordinary numeric source representation.
Example
module example.promoted_duration;
import std.time as time;
fn Main() -> i32 {
let Left = time.Duration<i8>.Create(2_i8);
let Right = time.Duration<i64, 1_u64, 1000_u64>.Create(250_i64);
let Sum: time.Duration<i64, 1_u64, 1000_u64> = Left + Right;
let Difference: time.Duration<i64, 1_u64, 1000_u64> = Left - Right;
if Sum.Count != 2250 || Difference.Count != 1750 { return 1; }
switch time.TryAddDurations(Right, Left) {
case .Ok { Value = Value } => { if Value.Count != 2250 { return 2; } }
case .Err { Error = _ } => return 3;
}
switch time.TrySubtractDurations(Right, Left) {
case .Ok { Value = Value } => { if Value.Count != -1750 { return 4; } }
case .Err { Error = _ } => return 5;
}
// Both promotion directions preserve checked overflow reporting.
if time.TryAddDurations(time.Duration<i8>.Create(1_i8), time.Duration<i16>.Create(32767_i16)) is .Ok { Value = _ } { return 6; }
if time.TryAddDurations(time.Duration<i16>.Create(32767_i16), time.Duration<i8>.Create(1_i8)) is .Ok { Value = _ } { return 7; }
return 0;
}