gleam-lang/gleam

Properly implement assignment patterns for `utf16` and `utf32` options in bit arrays

Open

#4566 opened on May 6, 2025

View on GitHub
 (1 comment) (1 reaction) (0 assignees)Rust (21,417 stars) (960 forks)batch import
discussionhelp wanted

Description

After implementing assignment patterns in bit arrays, we realised that we had not considered how to handle utf16 and utf32 options. For example with this code:

let assert <<"Hello" as message:utf16, _:bytes>> = <<"Hello, world!":utf16>>

What should the type of message be? There are two options: String and BitArray. With the same code, but replacing utf16 with utf8, message will have type String.

However, there is also an argument for returning a BitArray. Gleam strings are UTF-8, so converting a UTF-8 bit array into a Gleam string feels natural (on Erlang it is no-op). Converting a UTF-16 string to a bit array seems less intuitive, and less likely to be what the programmer wants 100% of the time.

The more we discussed this, the more we realised we had no actual data on how this was used. So we decided on a third option: error.

We don't want to make the compiler error in this case forever: it is likely that this will be a useful feature in the language at some point. But without any data, if we found out that we made the wrong decision, we could not change it as that would be a breaking change. Instead, we will wait until we get feedback from users about how they would like to use this feature, then implement it.

TL;DR

How would you expect the above code to function? What uses have you found for it? We appreciate the feedback!

Contributor guide