My First Impressions of Gleam
I’m looking for a new programming language to learn this year, and Gleam looks like the most fun. It’s an Elixir-like language that supports static typing.
I read the language tour, and it made sense to me, but I need to build something before I can judge a programming language well. I’m sharing some notes on my first few hours using Gleam in case they’re helpful to others learning Gleam or to the team developing the language. My project: Parsing old AIM logs
I used AOL Instant Messenger from about 1999 to 2007. For most of that time, I used AIM clients that logged my conversations, but they varied in formats. Most of the log formats are XML or HTML, which make re-reading those logs a pain. The simplest AIM logs are the plaintext logs, which look like this:
Every decade or so, I try writing a universal AIM log parser to get all of my old logs into a consistent, readable format.
Unfortunately, I always get bored and give up partway through. My last attempt was seven years ago, when I tried doing it in Python 2.7. Parsing logs is a great match for Gleam because some parts of the project are easy (e.g., parsing the plaintext logs), so I can do the easy parts while I get the hang of Gleam as a language and gradually build up to the harder log formats and adding a web frontend.
I’ve also heard that functional languages lend themselves especially well to parsing tasks, and I’ve never understood why, so it’s a good opportunity to learn. My background in programming languages
I’ve been a programmer for 20 years, but I’m no language design connoisseur. I’m sharing things about Gleam I find unintuitive or difficult to work with, but they’re not language critiques, just candid reactions.
I’ve never worked in a language that’s designed for functional programming. The closest would be JavaScript. The languages I know best are Go and Python.
How do I parse command-line args?
The first thing I wanted to do was figure out how to parse a command-line argument so I could call my app like this:
gleam myapp.gleam --arg1 arg2
But there’s no Gleam standard library module for reading command-line arguments. I found glint, and it felt super complicated for just reading one command-line argument. Then, I realized there’s a simpler third-party library called argv.
I can parse the command-line argument like this:
argv: String args = argv.split(" ") arg1 = args[1] arg2 = args[2]
What does the Gleam compiler do?
The biggest downside I’m finding with Gleam is that it’s a young language with a relatively small team. It just turned six years old, but it looks like the founder was working on it solo until a year ago.
There are now a handful of core maintainers, but I don’t know if any of them work on Gleam full-time, so the ecosystem is a bit limited. I’m looking ahead to parsing other log formats that are in HTML and XML, and there are Gleam HTML and XML parsers, but they don’t seem widely used, so I’m not sure how well they’ll work.
Gleam’s pipeline syntax
I love love love Gleam’s pipeline syntax. You can see me using it in the test with the |> characters:
parse_line |> result |> try |> Ok(result) |> else |> Error(nil)
The non-pipeline equivalent of the test would look like this:
result = parse_line() if (try result) { return Ok(result) } else if (isError(result)) { return Error(nil) }
It looks like wet garbage by comparison. Now that I’ve seen pipelines, they feel so obvious and conspicuously missing in every other programming language I use.
Built-in features
The first thing about Gleam that stood out to me is how few built-in features there are. For example, there’s no built-in feature for iterating over the elements of a List type, and the type itself doesn’t expose a function to iterate it, so you have to use the gleam/list module in the standard library.
Similarly, if a function can fail, it returns a Result type, and there are no built-in functions for handling a Result, so you have to use the gleam/result module to check if the function succeeded.
The future of Gleam
I’m enjoying Gleam. It pushes me out of my comfort zone the right amount where I feel like I’m learning new ways of thinking about programming but not so much that I’m too overwhelmed to learn anything.
Thanks to Isaac Harris-Holt for helpful feedback on this post.