lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, May 12, 2014 at 02:56:13PM +0200, Axel Kittenberger wrote:
> I'm just wondering, is this bad code since its using dummy '_' several
> times in nesting, or is that okay?
> 
> for _, a in ipairs( table ) do
> 
>    for _, b in ipairs( another ) do
> 
>        bla2( )
>    end
> end
> 
> Okay in the sense of changing the loop variable, since it may be ignored by
> ipairs anyway and running through (i remember tough that I read changing
> the loop variable is a bad idea) or okay since it is another _ in the inner
> loop and does not effect the outer dummy?

The inner _ is in an enclosing scope, and shadows the outer.  Accesses
to _ from the inside see the nested/shadowing one, accesses to _ from
the outer see the outer/shadowed one.

ie, perfectly safe, unless you happen to want to access the outer _ from
the inner scope.  In which case, give it a meaningful name and don't
shadow it :)

B.