Stream: beginners

Topic: List.last returns Result


view this post on Zulip Artur Swiderski (Apr 14 2023 at 18:18):

List.last returns Result what I supposed to do with it ? I need this last element not Result

view this post on Zulip Folkert de Vries (Apr 14 2023 at 18:20):

an empty list does not have a last element, this is why it returns a result

view this post on Zulip Folkert de Vries (Apr 14 2023 at 18:21):

you can pattern match on the output

when List.last myList is
    Ok last -> ...
    Err EmptyList -> ...

view this post on Zulip Artur Swiderski (Apr 14 2023 at 18:25):

thx, one thing when I try to pattern match Err [exact_error_name] -> it does not work

view this post on Zulip Folkert de Vries (Apr 14 2023 at 18:37):

I may have gotten that name wrong

view this post on Zulip Folkert de Vries (Apr 14 2023 at 18:38):

and using _ (the wildcard pattern) in that position is pretty common

view this post on Zulip Brendan Hansknecht (Apr 14 2023 at 18:58):

The correct error name is ListWasEmpty

view this post on Zulip Brendan Hansknecht (Apr 14 2023 at 18:59):

https://www.roc-lang.org/builtins/List#last

view this post on Zulip Richard Feldman (Apr 14 2023 at 20:06):

so it would be Err ListWasEmpty ->

coincidentally, one of the reasons for using named tags like this is so that the code can be self-documenting! :smiley:

view this post on Zulip Richard Feldman (Apr 14 2023 at 20:07):

e.g. if you have Err ListWasEmpty -> in your code instead of Err _ -> then the next person who comes along won't have to wonder when the Err case might come up, because the explanation is right there in the pattern match!


Last updated: Jul 06 2025 at 12:14 UTC