lua-users home
lua-l archive

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


On 10/5/11, Mark Hamburg <mark@grubmah.com> wrote:
> On Oct 2, 2011, at 12:55 AM, Lorenzo Donati wrote:
>
>> Very, true.
>>
>> This is IMO a big newbie gotcha: Lua tables are so "high-level", i.e. they
>> can be easily used to implement lists, trees, maps, stacks, etc., that one
>> can easily think they should provide a much fatter interface (the common
>> problem of getting into Lua's spirit).
>>
>> It's not easy to grasp initially that one can use OOP techniques to turn a
>> generic table into, say, a "stack object", with usual push/pop methods.
>
> Which leads to the argument that the "problem" is that Lua tables are too
> powerful. So often they are powerful enough that they just solve the
> problem. Much of the rest of the time, they seem powerful enough until you
> realize that they aren't quite. And then you come and complain to the list
> about tables being broken and about how Lua has to change. Maybe the change
> is that tables should be less powerful and people should be forced to roll
> their own data structures.
>
> Not that I buy that argument.
>
> Still, it does point to perhaps a getting started FAQ that would disabuse
> people of notions that will get them in trouble -- "You can do a lot with
> tables, but understand their limits and build data structures as you
> approach those limits."; "Metamethods are fallbacks not overrides and though
> they may seem like methods in an OOP language, they don't always behave that
> way."; etc. Then the real trick is to write this all without it turning into
> something like JavaScript: The Good Parts which contains a lot of warnings
> about the "bad" parts where JavaScript is just bizarre.
>
> Mark
>
>
>

Indeed. Tables are a corner stone of what Lua is and one of the
reasons why I like Lua.

My only problem with the "roll your own data structures" is how can
one do it without more basic blocks?

Imagine you want to build a tree datastructure where you can store an
arbitrary Lua value per node. How does one accomplish such a goal
without instanciating a bunch of tables in the process?

My question is not if it's possible, it's how can one do it efficiently.

Wouldn't a simple, fixed size, integer indexable (1 based, of course
;-) ), array type be better suited for the role of building block? I'm
talking about a new full-blown type here, with a metatable per
instance, just like a fixed size array part of a table without the
hash part.