Hi everyone,
Really quick question:
Is there a way of testing if a variable is a hash? “type(hash(“foo”))” only return “userdata” (same as url for example).
Thanks in advance,
K
Hi everyone,
Really quick question:
Is there a way of testing if a variable is a hash? “type(hash(“foo”))” only return “userdata” (same as url for example).
Thanks in advance,
K
No, there is no 100% certain way. Why do you need this?
I have created a function that takes a hash as a parameter and i would like to confirm the type before using it
Doesnt the go library use this kind of checks?
If you used something like ahash to generate hashes you could check to see if a hash value exists in its table.
Probably caching your hashes is the only way.
The best you can do is to check if it is of type userdata and leave it at that. I guess you could also do a pcall on hash_to_hex() maybe.
Sounds like perfect solution!
function is_hash(value)
return pcall(hash_to_hex, value)
end
function init(self)
local thing = hash("thing")
print(hash_to_hex(thing))
print(is_hash(thing))
print(is_hash(125))
print(is_hash("hello"))
end
Thanks, I’ll probably run with that
I feel like it could be useful for the defold engine to implement new returned values to type() though (since we are working heavily with hashes and urls for example).
Thanks for your help!