I don’t think we should change semantics of hd/1 as it comes from Erlang and 
can be used in guards.

Fortunately we have `List.first([])` and `Enum.drop([], 1)` as “safe" 
replacements.

> On 12 Dec 2023, at 13:19, waiting-for-dev <cram1...@gmail.com> wrote:
> 
> 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.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to elixir-lang-core+unsubscr...@googlegroups.com 
> <mailto:elixir-lang-core+unsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/elixir-lang-core/5af26219-331e-40f5-8a22-ab51b1039baen%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/elixir-lang-core/5af26219-331e-40f5-8a22-ab51b1039baen%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/C914D60C-E332-4847-AB59-5DCAC43BAB78%40wojtekmach.pl.

Reply via email to