LYR-SEM0222
a value boxed as an interface in a loop
In a loop, a number or a struct or enum value is passed where the parameter's type is an interface. Not said unless the lint is set to warn.
Explanation
An interface value holds a reference: a value becomes one by being copied into a box on the heap, each time it is passed — in a loop, each round. The lint is allow by default.
Take a type parameter constrained by the interface (fn show<T :: [Display]>(d: T)), which takes the value as it is, or box it once before the loop.
Wrong
fn show(d: Display): string {
return d.show();
}
@Warn("boxed-interface-in-hot-loop")
fn main(): void {
for (i in 0..3) {
let _ = show(i);
}
}
Right
fn show<T :: [Display]>(d: T): string {
return d.show();
}
@Warn("boxed-interface-in-hot-loop")
fn main(): void {
for (i in 0..3) {
let _ = show(i);
}
}