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 react-ts, vue-ts, or svelte-ts preset is also a great way to try making cross-platform GUIs with Tauri. It uses Vite under the hood, and the generated app is extremely minimal — nothing like the awful create-react-app behemoths that probably left a bad taste in everyone’s mouth.
> 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.
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 part of the appeal of Gleam for me.
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 = <
const V extends { kind: string },
const C extends { [ kind in V[ "kind" ] ]: ( value: V & { kind: kind } ) => unknown }
>( value: V, cases: C ) => cases[ value.kind as V[ "kind" ] ]( value ) as ReturnType<C[ V[ "kind" ] ]>
// 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
} )
}
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...
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.
The function call will fail at compile time if yourUnion is anything more than never, which you can use in your else case of if statements that narrow the discriminated union.
For those who is interested, as mentioned in the article, Gleam is already near v1 (v1.0.0-rc2)! And in a recent talk[1] Louis shared the idea, that there won't be major changes to the language anymore, and the main focus now is switching to the tooling!
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 react-ts, vue-ts, or svelte-ts preset is also a great way to try making cross-platform GUIs with Tauri. It uses Vite under the hood, and the generated app is extremely minimal — nothing like the awful create-react-app behemoths that probably left a bad taste in everyone’s mouth.