3 ms·
Take the following function: function arrmin<T extends {field: number}>(a: T[]): number { let m = a[0].field; for (let i = 1; i < a.length;
by tgv 3y ago
Take the following function:
function arrmin<T extends {field: number}>(a: T[]): number {
let m = a[0].field;
for (let i = 1; i < a.length; i++) {
if (a[i].field < m) {
m = a[i].field;
}
}
return m;
}
Now call arrmin([]). There's no way to avoid a throw. You would have to annotate every function that uses field selection on an array element (and a bunch of other conditions) as "throws", or enforce error checking code (if (0 <= i < a.length) {...}).
Rust didn't solve that either, does it? It just panics.