Index-based access on a list is very frequently an anti-pattern. Allowing
list[i] means it will be easier to write non-efficient versions of
algorithms without a second thought for those coming from imperative
languages, instead of using the functions in Enum. It means someone can
write this:

for i <- 0..length(list)-1 do
  list[i] * 2
end

We should not make it easy to write non-idiomatic code.

If we want to allow this syntax then I would like to see a comprehensive
list of examples where accessing a list by index is important  and doing so
via the bracket syntax is important enough to justify it.

On Thu, Sep 21, 2023 at 6:23 PM Zach Daniel <zachary.s.dan...@gmail.com>
wrote:

> Right now if you use access on a list, it expects it to be a keyword and
> requires the value to be an atom.
>
> I've tried to think of a reason why we couldn't support this in the
> language, but I haven't been able to. If the value being accessed is an
> `integer` it could get the item at that index, and if its an `atom` it
> could treat it as a keyword and get the value for that key.
>
> `[1, 2, 3][0] -> 1`
> `[foo: :bar][0] -> {:foo, :bar}`
> `[foo: :bar][:foo] -> :bar`
> `[1, 2, 3][:foo] -> not a keyword list error`
>
> It was pointed out that perhaps we don't do this to express that indexing
> a list is not fast in Elixir like it is in other languages, but I'm not
> sure if that is sufficient reason IMO to leave out a typically very
> standard feature of lists.
>
> Thoughts?
>
> --
> 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/dc7b3c13-08c7-48e8-a338-fe409ca4202an%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-core/dc7b3c13-08c7-48e8-a338-fe409ca4202an%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/CAGnRm4K6HdnLhde86jy1qETCCMqZLOX9q6%3D1dAxE8m_G81JjXg%40mail.gmail.com.

Reply via email to