lua-users home
lua-l archive

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


Bilyk, Alex wrote:
I also find this a problem, at least for myself. We have all sorts of
explanations why such is the behaviour of #, but It's just plain
counterintuitive to say the least. If not going into reading the
manual as to why such is the behavior, the results produced by the
cases below make no sense whatsoever and seem totally random. This
doesn't click well in my mind with the bulk of Lua, which I find
rather simple and intuitive.

I think the problems stem from the fact that a vector and a hash
under the hood are presented at language level as a single concept of
a 'table'. It would be easier to implement and maintain, it seems,
two separate language concepts of a vector and a hash, rather then
doing all sorts of acrobatics in attempts to present them via a
single language construct.

I apologize if this just adds noise to the conversation. But the way Javascript defines this is with a length property. If you ever add a numeric property to the object, it checks it against the length property and if it is greater, it sets length to i+1 (because Javascript starts indexes at 0). If, for instance, you add index 5 to an empty Array object, indexes 0-4 will be nil and the length will be 6. You can set and get length as well. Setting length to 4 on an empty object makes it have indexes 0-3, all with nil values. Setting length to less than its current value destroys all indexes at or above that new size.

This can be added on top of standard Lua, of course. In fact, when I switched to 5.1 last week, this is exactly what I did. It works fine and, since I do all the length fiddling on the C++ side, it is very fast. I still use lua_obj() to find out the size of a table that is being passed into one of my objects, but that is about it.

To me, the # operator and the lua_objlen() function are simply a convenient way to search for the last numeric property of an object.

--
chris marrin                ,""$,
chris@marrin.com          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'