|
On 2012-11-12 12:23 PM, "Egor Skriptunoff" <egor.skriptunoff@gmail.com> wrote:
>
> On 11/12/12, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> > Anyway, if you remove the outside declaration of a, then the code works
> > as intended:
> >
> > local print=print
> > do
> > local _ENV = require 'math'
> > print(pi)
> > end
>
> Let imagine that your source file is big enough, and sometime later
> you will forget about usage of math.pi and you will add new local
> variable at the beginning of the file:
>
> -- new code at the beginning:
> local pi = 40 -- plasticity index (of a soil)
> <new code concerning soil properties>
> -- your old code starts here:
> do
> local _ENV = require 'math'
> do_something(sin(pi*alpha/180)) -- sin is the same, but pi has changed!
> end
>
> And new pi will take precedence over pi from math module.
> How many time it will take you to debug your code to understand the reason?
> Figuratively speaking, _ENV changes lexical scoping from "wrong end".
> "With" statement would be absolutely correct here.
>
Well, and using more descriptive variable names (and especially taking care not to conflict with common mathematical constants) would also be wise...