Stream: ideas

Topic: ✔ Num.toBytes and Num.fromBytes


view this post on Zulip Kilian Vounckx (Jun 18 2023 at 16:02):

In the Num module at the moment there are functions for converting a list of bytes to unsigned integers. This set of functions could be extended to also convert integers to list of bytes. I think the user should also be able to specify endianness.
@Richard Feldman you had some thoughts?

view this post on Zulip Richard Feldman (Jun 18 2023 at 16:22):

yeah, so one thought is that my default preference would be that you have to explicitly specify either big-endian or little-endian

view this post on Zulip Richard Feldman (Jun 18 2023 at 16:22):

that way, we maintain the property that the same Roc code gives the same answer (or crashes) no matter the system it's run on

view this post on Zulip Richard Feldman (Jun 18 2023 at 16:23):

native endianness, like Nat (which #ideas > replace Nat concluded should be replaced with U64) violates that unless it's obtained as a Task like it is in Elm

view this post on Zulip Richard Feldman (Jun 18 2023 at 16:24):

so I think it's safe to assume we should support at least compiling to explicit endianness

view this post on Zulip Richard Feldman (Jun 18 2023 at 16:24):

but I think there's a reasonable question as to what use cases would want native endianness, and how big of a deal it would be if the only way to obtain that was via a Task

view this post on Zulip Kilian Vounckx (Jun 18 2023 at 16:37):

Richard Feldman said:

but I think there's a reasonable question as to what use cases would want native endianness, and how big of a deal it would be if the only way to obtain that was via a Task

I've never needed native endianness, so I don't think it would be a big deal. The only downside I see is that every platform would need to define it separately. Unless the proposal for language level tasks goes through of course

view this post on Zulip Kilian Vounckx (Jun 18 2023 at 16:38):

Actually, I don't know if language level tasks would help :sweat_smile:
But still, I don't think it is a big deal

view this post on Zulip Richard Feldman (Jun 18 2023 at 16:40):

language level tasks would allow it to be a builtin :thumbs_up:

view this post on Zulip Brendan Hansknecht (Jun 18 2023 at 17:13):

I think you just need a task to get the endianness of the current system. Then you save it /pass it down the call stack. Don't waste time going to the host for every conversion

view this post on Zulip Brendan Hansknecht (Jun 18 2023 at 17:15):

Where native endian is needed is going from a binary storage format to the systems format. If you have a proto that stores numbers in little endian and you run on a big endian system, you would need to convert to native get the correct integer.

view this post on Zulip Brendan Hansknecht (Jun 18 2023 at 17:15):

I think we should require specifying the endianness as well.

view this post on Zulip Brendan Hansknecht (Jun 18 2023 at 17:17):

Can roc have a built-in that generates a task? If so, i think a built-in task to get the endianness would be a great idea. No need to support it on every platform.

view this post on Zulip Brendan Hansknecht (Jun 18 2023 at 17:17):

I assume that feature might require future changes to task first

view this post on Zulip Richard Feldman (Jun 18 2023 at 17:53):

yeah totally - once Task is a builtin, we can just have Num.endianness (or Bytes.endianness; I've been noticing there are a lot of bytes-specific operations, and a Bytes module is sounding increasingly appealing) which returns Task Endianness *

view this post on Zulip Richard Feldman (Jun 18 2023 at 17:54):

Brendan Hansknecht said:

Where native endian is needed is going from a binary storage format to the systems format. If you have a proto that stores numbers in little endian and you run on a big endian system, you would need to convert to native get the correct integer.

ah, this is an interesting point! We could also separately have a way to convert from bytes that have a specific endianness into specific numbers in memory

view this post on Zulip Richard Feldman (Jun 18 2023 at 17:54):

which I don't think would require a Task, since you'd be explicitly specifying what endianness the input bytes have, and then once the number is in memory it's essentially opaque in terms of endianness

view this post on Zulip Notification Bot (Jun 18 2023 at 17:57):

Kilian Vounckx has marked this topic as resolved.

view this post on Zulip Brendan Hansknecht (Jun 18 2023 at 19:41):

Ah, so implicit native endian

view this post on Zulip Brendan Hansknecht (Jun 18 2023 at 19:41):

Cause that doesn't matter once loaded into a number type

view this post on Zulip Brendan Hansknecht (Jun 18 2023 at 19:41):

That is definitely a better api


Last updated: Jun 16 2026 at 16:19 UTC