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

 function __dispatchtable.setAttribute()
  attribute = param

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

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
|    | In uabo tererotitant
| ( | Brogoui sunt macresculi
+- --+ Momi rasti strugitant." --- Anonymous