lua-users home
lua-l archive

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


Just an arising question, why isn't it possible to pass a function as
first argument to a function without using brackets (like for tables
or strings). For example, I could write this helper function:
---
function with (env)
  return function (fn)
    setfenv(fn,env)
    return fn()
  end
end
---
and instead of:
---
t = {}
with (t) (function ()
  x,y,z = 1,23
end)
---
one could write:
---
with (t)
  function ()
     x,y,z = 1,2,3
  end
---
Or would there be an syntactical problem that I currently don't see?

Eike

2010/1/17 steve donovan <steve.j.donovan@gmail.com>:
> On Sat, Jan 16, 2010 at 7:10 PM, Mark Hamburg <mark@grubmah.com> wrote:
>>> setfenv(function()
>>>    x = 1
>>>    y = 2
>>>    g = function() return x+y end
>>>    z = 2
>>> end,t)()
>>>
>>> assert(t.g() == 3)
>>>
>> This, by the way, is a case where dynamic scoping would hurt. t.g() would generally result in an error because definitions for x and y could not be found.
>
> It works precisely because the new function g acquires the environment
> t. So as long as that's fine, it works (tested on Lua .5.1.4)
>