lua-users home
lua-l archive

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


> I note that all three people who responded on the mailing list
> objected to my use of the list data structure.  That surprises me.
> Lists are intrinsically ordered, tables are intrinsically
> unordered.  Lists take one object per node, tables take two
> objects, key and value, per node.  Lists and tables are just ...
> different.  Is there some reason lua people don't like lists?  Is
> there some unmentionable tar pit you are all trying to steer me
> away from?

Actually...that's not the issue, at least for me.  I use lists in my 
Lua code all the time.  There's nothing wrong with lists.  The 
objection (on my part) was implementing a list in C to be used by Lua 
-- there's no real benefit to that from what I can tell.  All the 
standard data structures like binary trees, lists, etc. are trivial to 
implement in Lua.

As a rule, I would only fallback to C when:

- calling a native API
- for performance intensive work
- for anything that requires intense bit twiddling
- for low level work like system/OS interfacing

Everything else I'd keep in Lua if possible.  In my own project, I 
only call back to C a handful of times (e.g. to call libpq).

> want, and has answered some specific questions.  One that he didn't
> answer is how to make type(obj) respond "list" if obj is a list.

Lua doesn't have user defined types.  Basically there are the basic 
data types (thread, number, table, string, etc.) and then tables.  Lua 
has no way of differentiating between two different tables 
structurally, at least at compile time.  It's a dynamic language.

This means that if you want something report its type, you'll probably 
want to make a base object "class" that returns a type string and have 
all your "derived" types report their type, and then write a wrapper 
function to unify that with the basic types.

function Object.create()
   local o = {}
   setmetatable( o, { __index = Object } )
   return o
end

function Object:get_type()
   return "object"
end

function List.create()
   local l = Object.create()
   setmetatable( l, { __index = List } )
end

function List:get_type()
   return "list"
end

function typestring( o )
   if o.get_type ~= nil then
      return o:get_type()
   else
      return type(o)
   end
end

Brian