Hacker News new | ask | show | jobs
by jondwillis 969 days ago
Why can’t provenance of throws be traced with certainty? It is possible to trace the return type of functions.

Even if it were an opt-in/progressive adoption keyword like “throws” that enforced some compiler guardrails, I think that could be a huge win for reducing unexpected runtime exceptions.

1 comments

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.