Gleam

I see today they released Gleam version 1. Let's try it!

Gleam is a BEAM language. That is, it runs on the Erlang abstract machine (the BEAM). I've already got that installed because Elixir needs it too.

❯ iex
Erlang/OTP 25 [erts-13.1.5] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.14.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>

Note that you can exit from iex (interactive Elixir) or erl (Erlang emulator) immediately with C-\ (that Ctrl+C advice is, er, sub-optimal).

If I didn't already have erlang, then I could install it on this Debian 12 box with

❯ sudo apt install erlang

Once we have a working BEAM, we can pick a Gleam release that matches our system.

❯ cd ~/Downloads
❯ wget https://github.com/gleam-lang/gleam/releases/download/v1.0.0/gleam-v1.0.0-x86_64-unknown-linux-musl.tar.gz
❯ wget https://github.com/gleam-lang/gleam/releases/download/v1.0.0/gleam-v1.0.0-x86_64-unknown-linux-musl.tar.gz.sha256
❯ sha256sum -c gleam-v1.0.0-x86_64-unknown-linux-musl.tar.gz.sha256 
gleam-v1.0.0-x86_64-unknown-linux-musl.tar.gz: OK

It's just a single executable file.

❯ tar tf gleam-v1.0.0-x86_64-unknown-linux-musl.tar.gz
gleam

Pick a convenient place to put it, like a binary directory that's already in your path.

❯ cd ~/.local/bin
❯ tar xf ~/Downloads/gleam-v1.0.0-x86_64-unknown-linux-musl.tar.gz

Easy peasy!

❯ gleam --version
gleam 1.0.0

How do we use it? There's a nice little guide on the Gleam site.

❯ gleam new vars
❯ cd vars
❯ gleam run
  Resolving versions
Downloading packages
 Downloaded 2 packages in 0.08s
  Compiling gleam_stdlib
  Compiling gleeunit
  Compiling vars
   Compiled in 1.64s
    Running vars.main
Hello from vars!

Okay, that's easy. Before we go much further, though, we'll need a gleam-mode for Emacs.

❯ cd
❯ mac gleam
❯ git clone --recurse-submodules https://github.com/gleam-lang/gleam-mode.git

❯ cat ~/.emacs.d/config/programming/gleam.el 
(use-package gleam-mode
  :load-path "~/gleam/gleam-mode")

That depends on the old tree-sitter.el

M-x package-install RET tree-sitter RET
M-x package-install RET tree-sitter-indent RET

That's too bad. Emacs 29 already includes treesit.el. Also, comment-dwim doesn't seem to work.

// foo

becomes

// // foo

then

// // // foo

So that's no good. Oh, but nosing around in that project reveals a gleam-ts-mode in a separate branch! That works in Emacs 29 straight away, without the need for those old tree-sitter packages.

❯ cd ~/gleam/gleam-mode
❯ git checkout gleam-ts-mode

❯ cat ~/.emacs.d/config/programming/gleam.el 
(use-package gleam-ts-mode
  :load-path "~/gleam/gleam-mode")

If we install the grammar from Emacs

M-x gleam-ts-install-grammar

Then that works. The first time we use comment-dwim, it doesn't know the comment characters to use, but once we tell it "//" all is well.

On Exercism, there's a nice Gleam track to follow. I think I'll give that a try!

./gleam-ts-mode.png

Also, there's a Gleam language server (gleam lsp), which I can start from Emacs with eglot but I'm not sure what it does yet. I have a lot to learn about both LSP and tree-sitter. I think we can have gleam-ts-mode offer to run gleam-ts-install-grammar for us, learn the comment characters, and who knows what else. And eglot should get all sorts of superpowers from gleam lsp.