[erlang-questions] Local namespaces in case/try
Tue Jul 10 13:03:36 CEST 2012
On 07/10/2012 12:52 PM, Sergei Lebedev wrote:
> Your argument makes sense, but I wonder why list comprehensions don't
> follow this design decision? Of course, from the implementation point
> of view the answer is obvious -- list comprehensions are compiled to
> functions, which indeed have a separate namespace. But for me, as a
> user, it's one more rule to learn; I think scoping rules should be
> consistent, especially if the goal is to keep Erlang semantics as
> simple as possible.
Funs and list comprehensions are indeed exceptions, and they do somewhat
complicate the language. Not everyone was happy when funs were added,
for example. I think funs tend to work because they are not so heavily
used, and you do get warnings about shadowed variables - something that
I personally thought was stupid to begin with, but eventually I realized
that taken with Erlang's normal scoping rules, it's best to avoid
variable shadowing to keep the code readable and maintainable.
When it comes to list comprehensions, I think that they were added a bit
too quickly without much peer review of the finer details. It is nice to
be able to have only local scope for variables bound in generators, but
on the other hand it trips people up sometimes. But list comprehensions
are well delimited visually and don't usually cause confusion.
More information about the erlang-questions