lua-users home
lua-l archive

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


Hello Lua list,

i want to create an object with full privacy. 
At the moment i have 4 approaches:

function privacy1(init)
  local data = init.data
  return function(m, arg)
    if m == "getdata" then return data end
    if m == "setdata" then data = arg end
  end
end

function privacy2(init)
  local data = init.data
  local function getdata() return data end
  local function setdata(arg) data = arg end
  return function(m)
    return nil
    or ("getdata" == m and getdata)
    or ("setdata" == m and setdata)
  end
end

function privacy3(init)
  local data, public = init.data, {}
  function public.getdata() return data end
  function public.setdata(arg) data = arg end
  return function(m)
    return public[m]
  end
end

function privacy4(init)
  local data, public = init.data, {}
  function public.getdata() return data end
  function public.setdata(arg) data = arg end
  return setmetatable({}, {__index = function(t,k)
return public[k] end, __newindex = function() error()
end})
end

Which of these approaches has the best performance for
objects with 20 - 30 methods?
How big must N be that one table lookup is faster then
executing N if-then blocks?  

Greets, marc


      __________________________________________________________
Gesendet von Yahoo! Mail.
Der Lieblings-Mailbox der Welt.
http://de.overview.mail.yahoo.com