List.last returns Result what I supposed to do with it ? I need this last element not Result
an empty list does not have a last element, this is why it returns a result
you can pattern match on the output
when List.last myList is
Ok last -> ...
Err EmptyList -> ...
thx, one thing when I try to pattern match Err [exact_error_name] -> it does not work
I may have gotten that name wrong
and using _ (the wildcard pattern) in that position is pretty common
The correct error name is ListWasEmpty
https://www.roc-lang.org/builtins/List#last
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:
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: Nov 09 2025 at 12:14 UTC