gleam-lang/gleam

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

Open

#4.566 aberto em 6 de mai. de 2025

Ver no GitHub
 (1 comment) (1 reaction) (0 assignees)Rust (960 forks)batch import
discussionhelp wanted

Métricas do repositório

Stars
 (21.417 stars)
Métricas de merge de PR
 (Mesclagem média 10d 19h) (69 fundiu PRs em 30d)

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!

Guia do colaborador