gleam-lang/gleam

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

Open

#4,566 建立於 2025年5月6日

在 GitHub 查看
 (1 留言) (1 反應) (0 負責人)Rust (960 fork)batch import
discussionhelp wanted

倉庫指標

Star
 (21,417 star)
PR 合併指標
 (平均合併 10天 19小時) (30 天內合併 69 個 PR)

描述

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!

貢獻者指南