Stream: contributing

Topic: List.range edge cases


view this post on Zulip Brendan Hansknecht (Feb 27 2023 at 20:20):

What should List.range {start: At 255u8, end: Length 2} do?

return [255, 0]?
panic?
return a result that is an error (in which case, the return type of List.range needs to change)?
other?

view this post on Zulip Joshua Warner (Feb 27 2023 at 20:31):

I would expect that to match the behavior of 255u8 + 2 (so, panic, I think?)

view this post on Zulip Brendan Hansknecht (Feb 27 2023 at 21:06):

Does the case feel exceptional enough to merit a panic as opposed to returning an error? I guess generally people don't use U8 with List.range so maybe.

view this post on Zulip Ayaz Hafiz (Feb 27 2023 at 21:07):

+1 on panic too

view this post on Zulip Brendan Hansknecht (Feb 27 2023 at 21:33):

I guess the same would apply to after? Though it does get more complex.

List.range { start: After 250u8, end: At 255 } -> [251, 252, 253, 254, 255]

List.range { start: After 250u8, end: At 255 , step: 10}
should panic? since 250 + 10 is an overflow? Or should it be an empty list []?

As an extra not, this works fine:
List.range { start: After 250u8, end: At 245 , step: 10}
It returns an empty list []. Since it is going in reverse. 250 - 10 = 240, which exits immediately.

view this post on Zulip Brendan Hansknecht (Feb 27 2023 at 21:34):

My gut feeling is that any call that doesn't use Length should never panic. It should just be an empty list because nothing is contained in the range. Length is special because you can actually run out of elements.

view this post on Zulip Ayaz Hafiz (Feb 27 2023 at 22:16):

that feels right to me too


Last updated: Jul 06 2025 at 12:14 UTC