lua-users home
lua-l archive

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


On Thursday 23 September 2004 16:18, Mark Hamburg wrote:
[...]
> Lacking a switch statement, however, method dispatch itself is slower.

Actually, I think you can get around that.

function makeObject()
 local attribute = 1
 local __dispatchtable = {}

 function __dispatchtable.attribute()
  return attribute
 end

 function __dispatchtable.setAttribute()
  attribute = param
 end

 return function (msg, param)
  local f = __dispatchtable[msg]
  if f then
   return f(param)
  else
   error("Unknown message: "..tostring(msg))
  end
 end
end

Untested, however...

However, this approach to implementing objects would make it rather hard to 
implement subclassing properly. You'd end up having to have seperate closures 
for each level in the inheritence chain and some foul hackery to make them 
all share the same dispatch table. Certainly possible, but I'm not sure if 
it's worth the effort... what sort of speed improvement are we looking at 
between table lookups and locals?

-- 
+- David Given --McQ-+ "Est brilgum: toui slimici
|  dg@cowlark.com    | In uabo tererotitant
| (dg@tao-group.com) | Brogoui sunt macresculi
+- www.cowlark.com --+ Momi rasti strugitant." --- Anonymous