lua-users home
lua-l archive

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


Big thanks!

But if that possible to avoid using = and not to create many function
like function owner/mode/group etc?

function dump(o)
  if type(o) == 'table' then
    local s = '{ '
    for k,v in pairs(o) do
      if type(k) ~= 'string' then k = '"'..k..'"' end
        s = s .. ''..k..' = \'' .. dump(v) .. '\', '
      end
    return s .. '} '
  else
    return tostring(o)
  end
end

function directory(name)
    local t = {name=name}
    return function (spec)
        for key,value in pairs(spec) do
            t[key] = value
        end
        print(dump(t))
--        create_directory(t)  ---> actually makes the directory
    end
end

directory "/etc" {
  path = "/etc";
  owner = "root";
  group = "wheel";
  mode = "0755";
}

2012/12/6 steve donovan <steve.j.donovan@gmail.com>:
> On Thu, Dec 6, 2012 at 8:53 AM, Vasiliy Tolstov <v.tolstov@selfip.ru> wrote:
>> 1-2) can you provide me simple exmple, please.
>
> function directory(path)
>     local t = {path=path}
>     return function (spec)
>         for key,value in pairs(spec) do
>             t[key] = value
>         end
>         create_directory(t)  ---> actually makes the directory
>     end
> end
>
> So it's a function that returnsa function - or more accurately creates
> a closure that has a reference to the local t (an 'upvalue')
>
> If we had "directory '/tmp/foo' { owner='root' }" then
> create_directory will be called with the table
> {path='/tmp/foo',owner='root'}
>
> steve d.
>



-- 
Vasiliy Tolstov,
Clodo.ru
e-mail: v.tolstov@selfip.ru
jabber: vase@selfip.ru