Stream: ideas

Topic: Safe Number calculations


view this post on Zulip Oskar Hahn (Dec 17 2023 at 20:45):

There is no try, expect in roc and I like this decision.

In most cases, this is not needed, since "unsafe" functions return a Result that can be checked in a safe way. For example to get the first element of a list is unsafe in a lot of langues. To write list[1] in go could panic. In roc, List.get list 1 can not.

But its different with numbers and overflow. I understand, that it would be very annoying, if 255 + 1 would return a Result. But on the other hand, it is annoying, to check values, before doing the calculation.

if x == 0 then
  Err ToSmall
else
  Ok (x - 1)

Would it be possible to add a Num.catch function, just to catch the panics, that come from calculations?

when Num.catch (x-1) is
  Ok n -> ...
  Err AdditionOverflow -> ...

Or is there another way how to handle the overflow errors without checking each value first?

view this post on Zulip Ayaz Hafiz (Dec 17 2023 at 20:57):

There’s a class of checked functions: https://www.roc-lang.org/builtins/Num#addChecked

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 21:52):

I assume the hope here is to group many ops and then check the result all at once? Wouldn't be super convenient, but the best way to do it would probably be backpassing with Result.try and the checked functions. Then just check the final result.

view this post on Zulip Brendan Hansknecht (Dec 17 2023 at 21:52):

Definitely a lot more verbose to get checking here

view this post on Zulip Oskar Hahn (Dec 17 2023 at 23:31):

I did know about these checked functions. Not so convenient, but I will give them a try

view this post on Zulip Notification Bot (Dec 18 2023 at 22:28):

7 messages were moved from this topic to #ideas > Better Backtraces by Brendan Hansknecht.

view this post on Zulip Brendan Hansknecht (Dec 19 2023 at 00:02):

Oskar, let us know how this goes, would love to see how the code works out. Maybe it will be really bad in terms of ergonomics and we should look at a way to making it automatic for number types. Would be nice to just brainstorm depending on ergonomics.


Last updated: Jun 16 2026 at 16:19 UTC