Live data from Hacker News

Building a hybrid native application with Gleam and Tauri

wezm.net

11–20 of 20 posts

Re: Building a hybrid native application with Gleam and Tauri

#11
post #9
post #8

Earlier quoted context omitted.

It has sum types! Type unions with a discriminator.

It kinda does if https://www.typescriptlang.org/play#example/discriminate-typ... is what you're referring to but it appears to be missing one of the major benefits: an error/warning if you haven't handled all variants. Edit: lots of replies showing how TypeScript can be made to do exhaustiveness checking. It's neat and all but it's a lot of gymnastics compared to languages that just have this built in, which again is…

Yeah, that can be annoying. Until pattern matching makes it into JS, the main way people deal with this is to use a library. For example, ts-pattern lets you stick `.exhaustive()` at the end of a match call and I believe you get the error at typechecking time, not runtime.

https://github.com/gvergnaud/ts-pattern

Re: Building a hybrid native application with Gleam and Tauri

#12
post #9
post #8

Earlier quoted context omitted.

It has sum types! Type unions with a discriminator.

It kinda does if https://www.typescriptlang.org/play#example/discriminate-typ... is what you're referring to but it appears to be missing one of the major benefits: an error/warning if you haven't handled all variants. Edit: lots of replies showing how TypeScript can be made to do exhaustiveness checking. It's neat and all but it's a lot of gymnastics compared to languages that just have this built in, which again is…

I ran into this exact issue when I was working on a piece of typescript that interoperates with a rust server, here's how I was able to do exhaustiveness checking (the errors aren't the prettiest, but it works): https://github.com/wwtos/mjuo/blob/main/vpo-frontend/src/lib...

EDIT: and an example of usage: https://github.com/wwtos/mjuo/blob/ca8c514185c1b5bb22aec752a...

Re: Building a hybrid native application with Gleam and Tauri

#13
post #9
post #8

Earlier quoted context omitted.

It has sum types! Type unions with a discriminator.

It kinda does if https://www.typescriptlang.org/play#example/discriminate-typ... is what you're referring to but it appears to be missing one of the major benefits: an error/warning if you haven't handled all variants. Edit: lots of replies showing how TypeScript can be made to do exhaustiveness checking. It's neat and all but it's a lot of gymnastics compared to languages that just have this built in, which again is…

You can workaround the lack of exhaustive matching with the following pattern:

    type Variant = { kind: "value", value: string  } | { kind: "error", error: string } | { kind: "unexpected" };

    class Unreachable extends Error {
        constructor(unexpected: never) {
            super(`${unexpected}`);
        }
    }

    function useVariant(variant: Variant) {
        switch (variant.kind) {
            case "value":
                return variant.value;
            case "error":
                return variant.error;
            default:
                throw new Unreachable(variant);
        }
    }
The `new Unreachable(variant)` will fail the type check only when you have not exhaustively matched all variants.

Re: Building a hybrid native application with Gleam and Tauri

#14
post #9
post #8

Earlier quoted context omitted.

It has sum types! Type unions with a discriminator.

It kinda does if https://www.typescriptlang.org/play#example/discriminate-typ... is what you're referring to but it appears to be missing one of the major benefits: an error/warning if you haven't handled all variants. Edit: lots of replies showing how TypeScript can be made to do exhaustiveness checking. It's neat and all but it's a lot of gymnastics compared to languages that just have this built in, which again is…

For the most straightforward nominative pattern matching, I would write a small match function:

    type A = { kind: "kindA", a: "dataA" }
    type B = { kind: "kindB", b: "dataB" }
    type Sum = A | B

    const match =  unknown }
    >( value: V, cases: C ) => cases[ value.kind as V[ "kind" ] ]( value ) as ReturnType

    // You check the type of result, change the type of value to A or B, make the cases non-exhaustive...
    const howToUse = ( value: Sum ) => {
        const result = match( value, {
            kindA: _ => _.a,
            kindB: _ => _.b
        } )
    }
You can test it here: https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeU...

Re: Building a hybrid native application with Gleam and Tauri

#15
post #4

Heads up, the link to the code repository at the top of the post 404d for me.

Oops, thanks for heads up, I had it set to private. Should be fixed now.

Also Celsius is the name of a guy, and you're misspelling it throughout the code base. :)

Re: Building a hybrid native application with Gleam and Tauri

#16
post #7
post #5

Recently learned about Gleam and am very interested in it as someone who always wanted to like Elixir but dislikes the Ruby syntax and lack of static typing. Love this approach to demonstrating the language in context. As a mostly frontend dev I feel it’s worth mentioning that while it’s a nice way to try Gleam, the Gleam aspect of this is inessential, TypeScript is quite good, and using create-tauri-app with the rea…

> the Gleam aspect of this is inessential, TypeScript is quite good TypeScript can be quite productive but I find its type system lacking. Specifically the use of structural typing/lack of nominal typing and lack of sum types. I find these extremely useful when writing reliable software, which is why Gleam appeals to me.

That's by all design.

Does this prevent you from writing reliable software?

Re: Building a hybrid native application with Gleam and Tauri

#17
post #5

Recently learned about Gleam and am very interested in it as someone who always wanted to like Elixir but dislikes the Ruby syntax and lack of static typing. Love this approach to demonstrating the language in context. As a mostly frontend dev I feel it’s worth mentioning that while it’s a nice way to try Gleam, the Gleam aspect of this is inessential, TypeScript is quite good, and using create-tauri-app with the rea…

Gleam's type system is just a Hindley-Milner type system. Which is tried, true and simpler, and extensively battle tested.

Typescript's type system isn't sound. Gleam's is.

Post reply on HN