lua-users home
lua-l archive

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


It's less of a problem when you write in purely Lua and extremely easy to work around, I agree.

In C++ it's a semi-regular thorn that reminds me when I spend too much time away from Lua. If I'm building something type-safe on top of Lua, using `false` to signal `nil` or similar does not exactly work properly. It's not impossible to work around, but convincing someone that serializing `nullptr` from C or C++ as `false`, some library-specific `USERDATA_NIL` (which is even harder to implement under 5.1 and 5.2 userdata comparison rules from metatable equality constraints), or other is an extremely hard sell. It's also even harder when someone places `nil` in a table that's expected to be a sequence and try to get that sequence out as, say, a `std::vector<my_type*>`. It just doesn't line up proper a lot with the types...

(Consequently, this is another reason why strict type safety is entirely optional in my opinion when building these things, which has earned me some grilling from colleagues before, but...)


On Fri, Jun 1, 2018 at 3:30 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2018-06-01 17:56 GMT+02:00 Russell Haley <russ.haley@gmail.com>:

> Well I've been at this for three years now and it's still a PIA for me and
> all the people that want to see an array data type. I *hate* having to use
> arrays in _javascript_, but I guess I'll have to throw my hat in that ring.
> NIL_IN_TABLE seems like a pretty minor compromise for allowing predictable
> sequences.

I've been in Lua, coming from Python, but further back from Pascal,
for seven years. The first two of them I spent lamenting the Python
data structures missing from Lua (i.e. lists, arrays and tuples, Lua
seemed to have only dictionaries). At the same time I gradually
mastered the more advanced features of Lua: metatables, generic
`for`, coroutines. Nowadays, I think in Lua.

On the rare occasions that I can't avoid writing Python, I spend half
of my time in the interactive help and the other half cursing the
non-equivalence of the invisible difference between a tab
and four spaces.

There is nothing involving arrays that you can't do with metatables
except write a table literal that will automatically imply a metatable.
Nobody compels you to use the table library. I have a whole file
of specialized tables: deque, stack, queue, list. An array is just
a special case of a deque, where upper and lower bounds are
variable.

But speaking of the table library: I wonder how many people are
using the very powerful 5.3 table.move. And how many people
have noticed the trivial implementation of nil-impervious
table.insert and table.remove that is possible with table.move.