I read in many topics what we can’t use hashes as a table keys in release builds.
If Project->Bundle->iOS Application… (set Release Mode checkbox) is the way to make a release build, then code below works in this build.
function init(self)
local h = hash("key")
print(h, type(h))
self.t = {}
self.t[h] = "value"
pprint(self.t)
end
function update(self, dt)
post("@render:", "draw_text", { text = self.t[hash("key")], position = vector3(100, 100, 0) } )
end
local tbl = {}
tbl[hash("hi")] = "mee"
msg.post(".", "test", tbl)
Another thing to remember is that there is no string representation of an hash in release. So don’t use tostring(some_hash) as it will differ from dev/release versions.
Is there any way to disable to_string in testing builds? I imagine it’s disabled in release due to performance problems - it might be useful to some people to leave out the lookup system for debug builds of intensive projects.
You can modify anything in Lua. So you don’t like randomness? Try:
math.random = function(m, n) return 12345 end
To few cats?
local _oldprint = print
print = function(...) _oldprint("Meow", ....) end
Anything goes…
It’s usually referred to as monkey patching. But remember, with great power comes great responsibility. If you’re writing code that you intend to share with others then it’s really bad if your code introduces side effects. And what if you expect a function to work in a certain way, and both you and some other library would try to modify the same function? On the other hand, it is super useful when writing unit tests and you wish to mock some behaviour, for instance replacing http.request with a static response or something like that.
In your example, is the only issue the `msg.post`? as in, the messaging process performs dark magic which breaks hash keys?
or
Is the usage of hashes as table keys in general a no no and there are other cases where it will explode?
Yes, it’s a limitation in the serialisation of the message data caused by how lua deal with table keys. They can be stored as values though. We should fix this at some point.