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

lexically scoped functions



Currently in es, you can not define a function that has lexical scope.  For
example, if you do

   let (fn foo { echo foo }) { foo }

you get a complaint that the function is undefined (assuming there is no
more global definition of foo). 

I asked Paul about this, and he says that at one time he had convinced
himself that lexical scoping for function names was not desirable, but no
longer remembers why now.

Here's why I think it's a useful feature.  Dynamically scoped functions may
have the undesirable property of shadowing functions where you don't want
them to.  Consider the following example:


      fn frobme  with some args { echo frobme $with $some $args }

      fn doit { 
         frobme harder $*
      }

      local (fn frobme { rm $* }) {
         frobme /tmp/*~
         doit to me baby
      }


Now, if internals of `doit' are unknown, you may unwittingly redefine some
function it relies upon to do something utterly different from your local
version.  In the above example, the consequences could be disasterous.

Certainly dynamic scope for functions is useful at times, but I think
lexical scope should also be allowed (it is certainly useful enough to
appear in most lexically scoped languages I know of).

What do people think?