I know that I can make my binding methods lowercase, but I would rather keep them the same as the method they are bound to (although it may be my fallback). I can try based on my Naming the standard construction method name, but not case sensitive and not easy to make mistakes.
I think there should be a tricky Lua film that can use metatables to solve this problem, but I can’t solve this problem myself.
p>
Any suggestions?
Thank you!
The best solution is to accept that you are dealing with a case-sensitive system, just like C, handle it.
However, if you really want to, you can do this. The easiest way is to put every possible case permutation of the name in the function table. So your function table will have this:
< /p>
["setname"] = theFunction,
["Setname"] = theFunction,
["sEtname"] = theFunction,
["SEtname"] = theFunction,
...
Of course, you can do this automatically with a function that takes each name in the table and copies its data according to the case arrangement.
A more complex but easier-to-use mechanism is to use the __index and __newindex meta methods and the empty table technique.
function CreateCaseInsensitiveTable()
local metatbl = {}< br />
function metatbl.__index(table, key)
if(type(key) == "string") then
key = key:lower()
end< br />
return rawget(table, key)
end
function metatbl.__newindex(table, key, value)
if(type(key) == "string") then
key = key:lower()
end
rawset(table, key, value)
end
local ret = {}
setmetatable(r et, metatbl)
return ret
end
You can use this function call to create a table instead of using {} to create a table. Otherwise, the table should function normally (although obviously member access Will be slightly slower).
I am using Lua as the data description language for my C application. I have a bunch of C classes that are bound to Lua using SLB 2.0. I have binding methods such as’SetPos’ or’SetName’. I use a table with a key value of “pos” or “name” to specify the position or name (for example). I want to be able to get the key, preceded by’set ‘, and call the method, if it exists (may not exist). Is it possible? If so, any suggestions?
I know that I can make my binding methods lowercase, but I would rather keep them the same as the method they are bound to (although it may be my fallback). I can try based on my Naming the standard construction method name, but not case sensitive and not easy to make mistakes.
I think there should be a tricky Lua film that can use metatables to solve this problem, but I can’t solve this problem myself.
p>
Any suggestions?
Thank you!
Case insensitivity is not something that Lua handles. All table lookups and local variable access are ultimately case-sensitive string comparisons.
< /p>
The best solution is to accept that you are dealing with a case-sensitive system, just like C, handle it.
But if you really want to, you can do this The easiest way is to put every possible case of the name in the function table. So your function table will have this:
["setname"] = theFunction,
["Setname"] = theFunction,
["sEtname"] = theFunction,
["SEtname"] = theFunction,
...
< p>Of course, you can do this automatically with a function that takes each name in the table and copies its data according to the case arrangement.
A more complex but easier-to-use mechanism is to use __index And __newindex meta method and empty table technique.
function CreateCaseInsensitiveTable()
local metatbl = {}
function metatbl.__index( table, key)
if(type(key) == "string") then
key = key:lower()
end
return rawget(table, key)
end
function metatbl.__newindex(table, key, value)
if(type(key) == "string") then
key = key :lower()
end
rawset(table, key, value)
end
local ret = {}
setmetatable(ret , metatbl)
return ret
end
You can use this function to call Create a table instead of using () to create a table. Otherwise, the table should run normally (although obviously member access will be slightly slower).