Stream: beginners

Topic: why are strings not just a distinct List U8


view this post on Zulip froge (Nov 01 2024 at 18:03):

so ive tried solving some aoc stuff in roc and compared to haskell having to convert to and from a list is kind of boilerplate-y, are there any specific reasons for strings not just being a distinct list of bytes

view this post on Zulip Brendan Hansknecht (Nov 01 2024 at 22:11):

For AOC I think most people should just directly use a List U8 and not attempt to use strings at all. Fundamentally, a Str is guaranteed to be valid utf-8. That adds extra constraints and restrictions. On top of that, it is exceptionally easy to misuse unicode and have subtle bugs. We have a partially built unicode library that enables more powerful operations over strings, but in general, we are trying to push people towards correct design when using strings and that often requires extra thought and constraints.

view this post on Zulip Brendan Hansknecht (Nov 01 2024 at 22:12):

For AOC, everything is ASCII. So it has no need to be a string at all.

view this post on Zulip Anton (Nov 02 2024 at 10:14):

We should really make an advent of code example that demonstrates the best practices, I've made an issue.

view this post on Zulip Oskar Hahn (Nov 03 2024 at 18:37):

I had the same question many times. If Str is an alias for List U8, all arguments for correct design would still be true. The Unicode package could still exist and work on Str/List U8.

There are many similar functions in the List and Str packets, that could be unified, if both types would be the same. And with the planned small list optimisation, there would be no different.

view this post on Zulip Brendan Hansknecht (Nov 03 2024 at 18:41):

Yeah, the only difference really is that str will be guaranteed to be correctly encoded utf-8

view this post on Zulip Brendan Hansknecht (Nov 03 2024 at 18:42):

It also attempts to restrict a few apis to make it more clear when someone might be using said utf-8 incorrectly. But currently those apis differences are pretty minimal


Last updated: Jul 06 2025 at 12:14 UTC