waiting-for-dev
unread,Dec 12, 2023, 7:19:44 AM12/12/23Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to elixir-lang-core
Kernel.hd/1 and Kernel.tl/1 raise an ArgumentError when given an empty list ([]). In my humble opinion, it would be beneficial to have safe versions of these functions, ideally following Elixir's naming convention: hd! and tl! for the raising variants, and hd and tl for the safe ones returning {:ok, term} or {:error, :empty_list}. I understand that this may not be a problem for writing expressive code, as we can always pattern match on lists instead of using these two functions. However, I believe it could be a potential issue for newcomers. Of course, the elephant in the room here is backward compatibility, but perhaps that could be considered for a major release?
For what it's worth, the same issue exists in the Haskell ecosystem, where the head and tail functions defined in the Prelude also raise errors on empty lists. This has been a persistent annoyance for the community, and many alternative Preludes ship with functions returning Maybe a values to address this. While the official Prelude in Haskell has never changed for backward compatibility reasons, in Haskell, you have the option to not import it, but that's not an option in Elixir.
Any ideas? I'm happy to hear everyone's thoughts on this. Thanks in advance for your feedback.