Hacker News new | ask | show | jobs
by jy3 2866 days ago
I'm not sure I understand the relationship between union-types and exceptions. You mean something like <Result, ErrorData>? Could you expand on that?

> Thanks to panics, even in Go.

Fortunately, for some reason Go developers don't use panic like exceptions and recover them at library boundaries.

1 comments

<Result, ErrorData>

Yes, exactly like that. In real world programs, every function might fail, even the simplest ones (stack overflow, out of memory, interrupts, etc). No information is gained by declaring a specific function might fail. Also it's almost certainly a lie to declare: this function will never fail. So if every function might fail, why not just produce a union type <Result|ErrorData> for every function return type. Also, lets automatically check for the error case after each nested invocation, and cleanly unwind the stack (returning ErrorData again) on failure. This makes 95% error handling code go away. The ErrorData type uses a special return keyword ("throw"), and in rare cases, errors need to actually be handled, so the union's ErrorData type is exposed to the user code with additional primitives (catch). On all the code in between, the ErrorData type is just hidden behind the scenes.