[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: reallycheckint(L,index)
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 29 May 2013 11:01:37 +0200
2013/5/29 Tim Hill <email@example.com>:
> I'm trying to figure out what you're checking (it's late here!) ..
> a Lua number that has no fractional part (with all the issues
> that can cause) or a Lua number that can be stored within
> a C "int" without overflow?
I am writing an __index/__newindex pair that checks the
"proper sequence" property: the numeric keys form the
exact set 1..n. However, the table is allowed to have
string keys (for example, `n` is one.) It was quite a shock
to discover that luaL_checkint does not actually check that
something is an integer.
But I have in the meantime thought of an elegant solution
to the problem. I create the table at the outset and fill it with
NaN. Then if the metamethod is invoked with a numeric key,
it can only mean the key is not in the table. That implies that
either it is out of the allowable set or that nil was at some
time assigned into the table, both of which are errors. And
I still have the telltale NaN to warn me that a key has not
had a value assigned to it.