I was trying to make a module that handled both go and gui objects, and needed to store references to each object. Normally I’d use go.get_id() but this is of course not available for gui scripts, so figured I would use msg.url() instead. I’m getting some strange behaviour and just wanted to check it’s as expected. I’m sure I’ll find another way to do this.
The above illustrates that you can set a table key as a url (because the data is visible in a pprint), but you can’t directly access the value by specifying the same url as a key.
An msg.url() is internally a what’s called a lightuserdata.
Among other things, it means that when it’s used as a “key”, the address of that lightuserdata is used. If you create two different instances, they’ll get different addresses.
That’s why your last line ( print(test[msg.url()])) won’t work as intended.
We have an issue to create a “key” from a url (e.g. something like url.to_key() which will give a single deterministic value based on the actual url values):
I was actually going down the same line myself, but without your intervention I would have come up with something like:
local urlkey = hash(url.socket .. url.path .. url.fragment)
That seems to work in my simple example.
So the key differences to your solutions are:
not catching instances where socket (or path or fragment) aren’t available - presumably causing errors in some situations (e.g. where any of the values are nil the string concatenation would fail)
not using hash_to_hex()
I’m quite on board with the first point (though I can’t imagine when it would happen) but I don’t understand the second one (not familiar with hash_to_hex() at all). Is it because string concatenations are slow, and using hash_to_hex() is faster?