News for hackers. Stuff that matters.
Re:TypeScript Is Surprisingly OK for Compilers by zkldi on Friday August 18, 2023 @04:15AM (#37172688)
that's not using `try` as an expression. can you do `let foo = try <code> (plus some handling for diverging in the other case?)`
Re:TypeScript Is Surprisingly OK for Compilers by kaba0 on Friday August 18, 2023 @04:36AM (#37172830)
It can and you in fact either have to mark that you return that error when you use try, or you have to provide a default value in a catch block:

``` fn potentiallyErrorReturningFunc() !u64 { ... }

const foo = potentiallyErrorReturningFunc() catch { 0 }; ```

One notable difference to most other languages is it being a value only, basically a product type of return value XOR error. These error values get serialized into a number by the compiler that is unique, and on the type system level you deal with sets of these error values. Quite clever in case of a low-level system, in my opinion.

Re:TypeScript Is Surprisingly OK for Compilers by flohofwoe on Friday August 18, 2023 @06:14AM (#37173602)
> const foo = potentiallyErrorReturningFunc() catch { 0 };

Nitpick, but AFAIK this won't work and needs to be rewritten either to:

    const foo = potentiallyErrorReturningFunc() catch 0;
...or to:

    const foo = potentiallyErrorReturningFunc() catch blk: { break :blk 0; }
(I wish there would be a more convenient way to return a value from a scope block, not necessarily with a "dangling expression" like Rust, but maybe at least getting rid of the label somehow)
Re:TypeScript Is Surprisingly OK for Compilers by flohofwoe on Friday August 18, 2023 @06:03AM (#37173512)
Yes, 'try' can be used like an expression in Zig which resolves to the unwrapped success-result of an error-union, or in case of an error immediately passes the error-result up to the calling function.