June 23rd, 2024

Elixir Gotchas

The article highlights common pitfalls in Elixir programming, including confusion between charlists and strings, differences in pattern matching, struct behavior, accessing struct fields, handling keyword lists, and unique data type comparisons.

Read original articleLink Icon
Elixir Gotchas

This article discusses 10 common pitfalls encountered when working with Elixir, a programming language built on Erlang. The first issue highlighted is the confusion between charlists and strings, where single-quoted strings are lists of integers while double-quoted strings are UTF-8 encoded binaries. The article also delves into pattern matching in Elixir, showcasing differences between lists and maps. It emphasizes that structs in Elixir are essentially maps with a special key. The article warns about the lack of Access behavior in structs, advising the use of dot-syntax or Map functions for accessing struct fields. Additionally, it touches on the peculiarities of keyword lists in Elixir, which are used for passing options to functions but can be challenging to pattern match due to their list nature. The piece concludes by noting Elixir's unique feature of allowing comparisons between different data types without raising errors, following Erlang's structural comparison approach.

Link Icon 6 comments
By @sph - 5 months
This would be more helpful if it dove into the Erlang side, as many of these "gotchas" are due to how Erlang and the BEAM work, and Elixir, being built on top of it and binary-compatible with the entire ecosystem, necessarily exhibits these behaviours.

Keyword lists, charlists vs strings (actually called binaries) [1], empty map matching all maps are obvious if you look at how they work in Erlang.

I haven't written a single line of Erlang though I can grok it pretty well by now. Knowing the entire platform is how you become a productive Elixir programmer, though it seems some devs refuse to look behind the curtains to understand the Erlang side of it, thus never truly understand the power of the BEAM platform.

---

1: I admit I still unsure about the separation of charlists and binaries. As far as I understand it, binaries are stored in their own heap to avoid copying when sending across processes, so are ideal for sharing large payloads, and charlists are not great for UTF-8 content, as they simply are a naive list of integers (hence one of the gotchas). Erlang uses charlists a lot for "string" content.

Elixir made the right choice to use binaries for strings, as they can ensure they are correctly encoded, provide high-level API to deal with the intricacies of Unicode, and still retain compatibility with Erlang by providing charlists and raw binaries for non-UTF-8 data.

By @arrowsmith - 5 months
This is slightly out of date:

> The other mystery here are the 2 different syntaxes in use for charlists – single quotes ('charlist') vs. the ~c"charlist" sigil. That’s a rather recent development, it was changed in Elixir 1.15, after some discussion … > > So, it’s now less confusing but still confusing – which is why it made this list.

Charlists with single quotes are deprecated since Elixir 1.17 - the ~c[…] sigil should always be preferred.

So hopefully that will reduce the confusion!

By @fredwu - 5 months
Are these even gotchas if they are all very well documented in the official documentation already? I was expecting to see some unusual or undocumented behaviours...
By @robxorb - 5 months
Unless you are fine letting old homebrew work its "magic" - the installation is too lengthy and involved for a new language. It should just be click, download:

https://elixir-lang.org/install.html

That is the biggest gotcha for me. There doesn't seem to be a simple installer or binary you just get up and running unlike other major languages. Is it a distribution issue with erlang or something? I'd have thought it'd be easy to package it up.

By @cutler - 5 months
Elixir also doesn't have arrays so you have to fall back on Erlang's extremely clunky variant or, worse, shoehorn a map.