|
On 09/04/2014 11:09 AM, Milind Gupta wrote:
It gets even more peculiar. If I print the table keys and values the key==userdata evaluates to true but accessing the table with each of them gives different results i.e. data[key] ~= data[userdata]. I have attached the updated script.
On Wed, Sep 3, 2014 at 7:20 PM, Milind Gupta <milind.gupta@gmail.com <mailto:milind.gupta@gmail.com>> wrote:
Sorry, the right attachment is this one:
On Wed, Sep 3, 2014 at 7:15 PM, Milind Gupta <milind.gupta@gmail.com <mailto:milind.gupta@gmail.com>> wrote:
Hi,
I am using the element handle of a multiline text control as a key in a table (data). But when the table is accessed in the map_cb function (called when the control is mapped) the value for that key is somehow lost!
I have the test case in the attached script. The print statements show that the right key is passed to the map_cb function but the table shows a nil value at that time.
What am I doing wrong?
Thanks,
Milind
Lua looks up a table using the key in the `raw` manner. if you check the raw equality,
i.e. change ``k==term`` into ``rawequal(k, term)``, it will print out `false`. that would make sense.
iuplua stores a pointer to the underlying ihandle via a full userdata, and associate a metatable with
the userdata, containing metamethods such as __tostring, __eq, __index, and __newindex, and so on.
thus there are actually two levels of indirection to access a GUI element. the metamethod _eq checks
for the equality of the underlying ihandle values, but Lua table looking up checks for the equality of
the userdata values (i.e. pointer to ihandle) themselves.
here I could think of several solutions.
1. store your data directly into the GUI element object. IUP allows arbitrary attributes to be associated with
a ihandle. in the Lua syntax, just write
term.data = ""> instead of
data[term] = <...>
2. if for certain reasons you must store the data externally looked up in a table, don't use the Lua value
directly as keys. one possible way is to use the string representation (i.e. tostring(term)) as keys:
data[tostring(term)] = <...>
3. or you may use a hybrid method, something like:
term.__mykey = {}
data[term.__mykey] = <...>
I personally prefer method 1.
P.S. the IUP Ihandle structure has its own hash table implementation. I sometimes think that maybe we
could design another GUI library bases upon Lua (not binding to Lua) so that we could make use of Lua's
hash table algorithm instead of re-implement (or even re-invent) another one.