Experiment: Application with Elixir, business logic with Gleam
by Paweł Świątkowski
13 Oct 2021
One of the young programming languages I look at from afar and root for is Gleam. It is a statically typed language for BEAM, written in Rust. Similarily to Rust, if focuses on catching many potential bugs at compilation time and providing useful error messages, so it’s easy to fix them. Since it’s targetting BEAM, it can easily interop with other BEAM languages, such as Elixir or Erlang. And this fact allows to overcome some issues that languages in their infancy stage usually have.
My recent thought revolve around using Gleam to model the core business logic, while letting Elixir do the heavy application work around it. Let’s face the facts: Phoenix is great, Ecto is great, ExUnit is great… It will take years for language such as Gleam to develop ecosystem similar to Elixir. On the other hand, I find modelling business logic with types easier and more maintainable than without them. And Elixir typespecs have their issues:
- Dialyzer error messages are not very friendly, it usually takes me couple minutes to figure out what they really mean.
- Types are optional and there seems to be almost-consensus to write typespecs for public functions only. However, in my experience, because of that, bugs happen a lot in private functions, which are neither typespec’d, nor unit tested.
- Dialyzer simply does not catch everything. In fact sometimes I’m surprised at the things it does not catch.
In Gleam, all these issues are addressed: types are mandatory in every function, the compiler checks everything and error messages are quite informative. For example:
So I decided to have a look at how the interop between Gleam and Elixir works and if it could be useful. To set things us, I used mix_gleam Hex package from Gleam team, which allows you to compile Gleam as part of Mix compilation. I just followed instructions in the readme and everything works. So, time to write the first “business logic” in Gleam.
To test things, I decided to code a fragment of super simple booking system for my imaginary estate. It’s quite small - only has three rooms. But still, you can book them, if the details are valid. Here’s the relevant code:
It’s not exactly Elixir, but I guess you can get what’s going on, as it’s quite idiomatic.
When compiled, Gleam modules are available like Erlang modules. So I can write this, for example:
As it turns out, Gleam types, when returned to Elixir, become a tuple with first element being a type name and the following elements field values. This works both ways, so I can pipe the result into another Gleam function.
This looks pretty good. A nice thing is that Gleam’s Result
type maps elegantly into Elixir’s {:ok, value}
or {:error, message}
convention.
Maybe I didn’t see a lot, but it brings me close to the conclusion that yes, Gleam can be mixed with Elixir in parts where full static typing is desirable and integration between the two languages goes pretty seamless. I would have to prepare a larger example to check it, but potentially looks good.
A few things I don’t necessarily like in Gleam (or find missing):
- Completely unlike Elixir, it relies heavily on filenames - i.e. name of the file is the name of the resulting module. This is not necessarily bad, but I prefer less coupling between the filesystem and the code.
- No support for multiple dispatch or default arguments forces me to write weird things like
new2
andnew3
pseudo-constructors. Perhaps in real example it wouldn’t be so problematic though, as probably I’d be passing all the data at once. - I was surprised that sometimes the compiler wasn’t able to infer the type, even though it should be possible.
- The standard library at the moment does not include things like dates at all, although of course I can define relevant logic myself without a lot of problems.
Bonus content: Accessing namespaced Gleam modules
I wrote above that module names accessible to Elixir are named based on the filename of the file where Gleam code is written. It was pretty straightforward when I did it in src/booking.gleam
files and could access via :booking.new()
, but what if I had a file in a subdirectory? I checked it by creating a src/transport/trains.gleam
file and couldn’t really find a way to call it from Elixir. I was close to the conclusion that it’s impossible and you can only write publicly exported code in top-level namespace, but then I found how to list all the atoms defined for the application:
So, in short, you use @
-sign as a namespace separator.
And this is the code of trains
module if you’re interested: