Changing metatable in Lua breaks colon operator - lua

While learning Lua, I borrowed some code from here
to use string indexing, which is exactly this:
getmetatable("").__index = function(str, i) return string.sub(str, i, i) end
After that, I wrote a function to reverse a string as practice.
function reverse_string(str)
local s = ""
for i = string.len(str), 1, -1 do s = s .. str[i] end
return s
end
That works fine, until I change the string.len(str) to str:len(), then I get this error:
reverse.lua:9: bad argument #2 to 'sub' (number expected, got string)
Debugging print()'s tell me that the __index function is being called on str:len(), and that the i argument is becoming the string "len". I know that str:len() works without the metatable there, but as soon as I add it this happens, why?

From Lua 5.2 Refernce Manual:String Manipulation
The string library provides all its functions inside the table string. It also sets a metatable for strings where the __index field points to the string table. Therefore, you can use the string functions in object-oriented style. For instance, string.byte(s,i) can be written as s:byte(i).
So, the object oriented style like str:len() comes from the default metamethod __index, which you have modified.

The index function is passed the table, and the key that is being indexed. so 'str' should be the string and 'i' should be key in your case. Because "len" is not in the meta table it calls __index and passes the string as the first argument and the key ("len") as the second argument. it looks as though you need to check to the type of 'i' to see what to do for better handling of strings
getmetatable("").__index = function(str, key)
if type(key) == "string" then
return string[key]
else
return string.sub(str, key, key)
end
end
str = "hello, world!"
print(str:len())
print(str[5])
see here for more info

Related

function arguments expected

I'd like to accomplish one thing with metatablitz, but i don't understand how to do it right. More precisely, i implemented it, but i miss one little thing - so that in the right place i can use a colon.
The code below is simple and speaks for itself. How do i make sure that Z:Entity.Create() does not cause an error?
Z = {
Entity = {
Create = function(name)
print ('Entity name:', name)
end,
}
}
setmetatable(Z.Entity, {__call = function(self, ...)
print (...)
end})
local p1 = Z:Entity('New entity') -- table: 0xb8f730 New entity
p1 = Z.Entity.Create('test') -- Entity name: test
p1 = Z:Entity.Create('test') -- lua: [string "<eval>"]:16: function arguments expected near '.'
Any advice or help would be welcome!
a:name() is syntactic sugar for a.name(a)
So if you write Z:Entity.Create('test'), the interpreter expects Entity to be a function value. But instead of the expected function arguments in parenthesis there is a dot.
Your code doesn't make too much sense though. Why do you assign three different things to p1?
Your code isn't really self explanatory, you need to explain what you are trying to do and why you want to use a colon. I recommend reading up on the syntax, maybe with this question. The colon basically is a shortcut for calling a function on an object and passing the object as the first parameter. When defining a function using the colon syntax it lets you skip declaring the 'self' parameter and declares one behind the scenes.
Let's look at your three sample statements:
First statement:
local p1 = Z:Entity('New entity')
That is equivalent to this:
local p1 = Z.Entity(Z, 'New entity')
Since you set a metatable for Z.Entity with __call, you can use call the Entity table as a function. Since you use the colon syntax, it passes Z as the first parameter when calling the function so you are really passing two parameters. The way it is declared then, self will be Z.Entity and ... will expand to the two arguments, Z and 'New entity'.
Second statement:
p1 = Z.Entity.Create('test')
Here you are simply calling the function 'Create' and passing it the string 'test' prints out Entity name: test. You are not using colon syntax and the metatable __call function isn't being executed because you are not calling Entity like a function.
Third statement:
p1 = Z:Entity.Create('test')
-- lua: [string "<eval>"]:16: function arguments expected near '.'
When you write Z:Entity, it expects you to be calling it as a function and it will pass Z as the first argument to that function. It doesn't make any sense to have a period following Entity.
Possible assumption
From the name Entity.Create, it sounds like you want to create a new object with this function and return it. This is where you would normally return a new table and call setmetatable and specify __index which will look for the the passed object to find functions that can be called. You should read up on object oriented programming in LUA, but here's a quick example of what I think you're trying to do:
Z = {
Entity = {
SayHello = function(self)
print(self.name .. " says 'Hello'!")
end,
Create = function(name)
local obj = {}
obj.name = name
setmetatable(obj, {__index = Z.Entity})
-- now you can call obj:SayHello()
return obj
end
}
}
local p1 = Z.Entity.Create('Player 1')
-- here p1 is passed as the first argument to SayHello because
-- a colon is used
p1:SayHello() -- Player 1 says 'Hello'!

How to create a tables with variable length with string-like keys in lua

I have a file database. Inside that file I have something like:
DB_A = ...
DB_B = ...
.
.
.
DB_N = ...
I would like to parse the data and group them in lua code like this:
data={}
-- the result after parsing a file
data={
["DB_A"] = {...},
["DB_B"] = {...},
.
.
.
["DB_N"] = {...}
}
In other words, is it possible to create a table inside a table dynamically and assign the key to each table without previously knowing what will be the names of the key (that is something I can figure out after parsing the data from a database).
(Just as a note, I am using Lua 5.3.5; also, I apologize that my code resembles C more than Lua!)
Iterating through your input file line-by-line--which can be done with the Lua FILE*'s lines method--you can use string.match to grab the information you are looking for from each line.
#!/usr/bin/lua
local PATTERN = "(%S+)%s?=%s?(%S+)"
local function eprintf(fmt, ...)
io.stderr:write(string.format(fmt, ...))
return
end
local function printf(fmt, ...)
io.stdout:write(string.format(fmt, ...))
return
end
local function make_table_from_file(filename)
local input = assert(io.open(filename, "r"))
local data = {}
for line in input:lines() do
local key, value = string.match(line, PATTERN)
data[key] = value
end
return data
end
local function main(argc, argv)
if (argc < 1) then
eprintf("Filename expected from command line\n")
os.exit(1)
end
local data = make_table_from_file(argv[1])
for k, v in pairs(data) do
printf("data[%s] = %s\n", k, data[k])
end
return 0
end
main(#arg, arg)
The variable declared at the top of the file, PATTERN, is your capture pattern to be used by string.match. If you are unfamiliar with how Lua's pattern matching works, this pattern looks for a series of non-space characters with zero or one spaces to its right, an equal sign, another space, and then another series of non-space characters. The two series of non-space characters are the two matches--key and value--returned by string.match in the function make_table_from_file.
The functions eprintf and printf are my Lua versions of C-style formatted output functions. The former writes to standard error, io.stderr in Lua; and the latter writes to standard output, io.stdout in Lua.
In your question, you give a sample of what your expected output is. Within your table data, you want it to contain keys that correspond to tables as values. Based on the sample input text you provided, I assume the data contained within these tables are whatever comes to the right of the equal signs in the input file--which you represent with .... As I do not know what exactly those ...s represent, I cannot give you a solid example for how to separate that right-hand data into a table. Depending on what you are looking to do, you could take the second variable returned by string.match, which I called value, and further separate it using Lua's string pattern matching. It could look something like this:
...
local function make_table_from_value(val)
// Split `val` into distinct elements to form a table with `some_pattern`
return {string.match(val, some_pattern)}
end
local function make_table_from_file(filename)
local input = assert(io.open(filename, "r"))
local data = {}
for line in input:lines() do
local key, value = string.match(line, PATTERN)
data[key] = make_table_from_value(value)
end
return data
end
...
In make_table_from_value, string.match will return some number of elements, based on whatever string pattern you provide as its second argument, which you can then use to create a table by enclosing the function call in curly braces. It will be a table that uses numerical indices as keys--rather than strings or some other data type--starting from 1.

Lua - get table hex identifier

I want to know how to get the table hex id. I know that doing:
local some_var = {}
print (some_var)
the result is (for instance):
table: 0x21581c0
I want the hex without the table: string. I know that maybe some of you suggest me to make a regular expression (or something similar) to remove those chars, but I want to avoid that, and just get the 0x21581c0
Thanks
This is simpler and works for all types that are associated with pointers:
local function getId(t)
return string.format("%p", t)
end
print("string:", getId("hi"))
print("table:", getId({}))
print("userdata:", getId(io.stdin))
print("function:", getId(print))
print("number:", getId(1))
print("boolean:", getId(false))
print("nil:", getId(nil))
Result:
string: 0x0109f04638
table: 0x0109f0a270
userdata: 0x01098076c8
function: 0x0109806018
number: NULL
boolean: NULL
nil: NULL
In the standard implementation, there is the global 'print' variable that refers to a standard function that calls, through the global variable 'tostring', a standard function described here. The stanard 'tostring' function is the only way to retrieve the hexadecimal number it shows for a table.
Unfortunately, there is no configuration for either of the functions to do anything differently for all tables.
Nonetheless, there are several points for modification. You can create you own function and call that every time instead, or point either of the the global variables print or tostring to you own functions. Or, set a __tostring metamethod on each table you need tostring to return a different answer for. The advantage to this is it gets you the format you want with only one setup step. The disadvantage is that you have to set up each table.
local function simplifyTableToString(t)
local answer = tostring(t):gsub("table: ", "", 1)
local mt = getmetatable(t)
if not mt then
mt = {}
setmetatable(t, mt)
end
mt.__tostring = function() return answer end
end
local a = {}
local b = {}
print(a, b)
simplifyTableToString(a)
print(a, b)
Without complex patterns, you can just search for the first space, and grab the substring of what follows.
function get_mem_addr (object)
local str = tostring(object)
return str:sub(str:find(' ') + 1)
end
print(get_mem_addr({})) -- 0x109638
print(get_mem_addr(function () end)) -- 0x108cf8
This function will work with tables and functions, but expect errors if you pass it anything else.
Or you can use a little type checking:
function get_mem_addr (o)
return tostring(o):sub(type(o):len() + 3)
end
The table id stated by the OP is invalid in the version of Lua I am using (5.1 in Roblox). A valid ID is length 8, not 9 as in your example. Either way, just use string.sub to get the sub-string you are after.
string.sub(tostring({}), 8)
The reason is, 'table: ' is 7 characters long, so we take from index 8 through the end of the string which returns the hex value.

Lua: When is it possible to use colon syntax?

While I understand the basic difference between . and :, I haven't fully figured out when Lua allows to use the colon syntax. For instance, something like this does work:
s = "test"
-- type(s) is string.
-- so I can write a colon function for that type
function string:myFunc()
return #self
end
-- and colon function calls are possible
s:myFunc()
However the same pattern does not seem to work for other types. For instance, when I have a table instead of a string:
t = {}
-- type(t) is table.
-- so I can write a colon function for that type
function table:myFunc()
return #self
end
-- Surprisingly, a colon function call is not not possible!
t:myFunc() -- error: attempt to call method 'myFunc' (a nil value)
-- But the verbose dot call works
table.myFunc(t)
Moving on to another type:
x = 1
-- type(x) is number.
-- So I was expecting that I can write a colon function
-- for that type as well. However, in this case even this
-- fails:
function number:myFunc()
return self
end
-- error: attempt to index global 'number' (a nil value)
I'm currently trying to make sense of this. Is it correct to conclude that
certain types like string allow both colon-function-definitions and colon-function-calls.
other types like table allow only colon-function-definitions but not colon-function-calls.
yet other types like number allow neither.
What exactly is the reason for these differences? Is there a list of all types, showing which type of colon-syntax they support? Is there maybe a workaround for the number case, allowing to write e.g. x:abs()?
The first example on string works because, all strings share the same metatable, and it's stored in the table named string.
From string:
The string library provides all its functions inside the table string. It also sets a metatable for strings where the __index field points to the string table. Therefore, you can use the string functions in object-oriented style. For instance, string.byte(s,i) can be written as s:byte(i).
The second example on table doesn't work because, every table has its own metatable, the table named table is just a collection of all the functions of table library.
Types like numbers don't support metatable by default.
As a total Lua newbie, it took me a while to understand #Yu Hao's answer, so I'll try to add some details for other beginners. Please correct me if anything is wrong.
As far as I can see, a call like x:someFunc() works if [*]:
x has a metatable
and the metatable has a field __index
which points to a table containing a function someFunc.
As Yu Hao has pointed out, strings automatically get a metatable pointing to the table string, e.g.:
th> s = 'test'
th> getmetatable(s)
{
__mod : function: 0x40c3cd30
__index :
{
upper : function: builtin#82
rep : function: builtin#79
split : function: 0x40ffe888
gfind : function: builtin#87
find : function: builtin#84
reverse : function: builtin#80
lower : function: builtin#81
len : function: 0x40af0b30
tosymbol : function: 0x40ffe8a8
myFunc : function: 0x41d82be0 -- note: this comes from our custom function string:myFunc()
dump : function: builtin#83
byte : function: builtin#76
char : function: builtin#77
gmatch : function: builtin#87
match : function: builtin#85
sub : function: builtin#78
gsub : function: builtin#88
format : function: builtin#89
}
}
So in this case s:myFunc() works automatically. In order to use the colon syntax for a table, we can manually set its metatable:
th> function enableColonForTable(t)
..> meta = {__index = table}
..> setmetatable(t, meta)
..> end
th> t = {}
th> enableColonForTable(t)
th> t:insert(1) -- works now!
Another observation is that it actually does not matter whether __index points to a table with exactly the same name as the type. Instead of meta = {__index = table}, we also could do:
th> arbitraryScope = {}
th> function arbitraryScope:test() return "something" end
th> t = {}
th> setmetatable(t, {__index = arbitraryScope})
{}
th> t:test()
something
That is also the key difference to the case of a number. While there are existing tables called string and table, there is no existing table called number. This is why even defining e.g. function number:abs() has failed before. But we can still make it work:
th> number = {}
th> function number:abs() return math.abs(self) end
th> x = -123
th> debug.setmetatable(x, {__index = number})
-123
th> x:abs()
123
Note that we had to use debug.setmetatable instead of setmetatable here. The difference between the two seems to be that setmetatable sets the metatable only for the given instance, while debug.setmetatable sets the metatable for the whole type. Apparently setting an individual metatable for numbers is forbidden (and would not make much sense anyways). This means that (in contrast to tables) newly constructed numbers now have the given metatable by default, so this works:
th> y = -42
th> y:abs()
42
[*] Update
As pointed out by Tom Blodget, x:someFunc() also works if x itself serves as a namespace, i.e., it is a table with a method field someFunc. For instance you could do table:insert(1). But now the namespace (the table called table) is passed as self and you would have added data to the namespace:
th> print(getmetatable(table)) -- note: "table" does not have a metatable
nil
th> table:insert(1) -- yet a colon syntax call works
th> table
{
prune : function: 0x4156bde0
getn : function: 0x41eb0720
maxn : function: builtin#90
remove : function: 0x41eb08c8
foreachi : function: 0x41eb05b8
sort : function: builtin#93
concat : function: builtin#92
unpack : function: builtin#16
splice : function: 0x4156bdc0
foreach : function: 0x41eb0688
1 : 1
pack : function: builtin#94
insert : function: builtin#91
}
Supplementary answer:
First, note that a function is a value (aka "first-class citizen").
: is one of three indexing operators in Lua. An indexing operator returns the value of a "field" from an object that can be indexed—be it a function or any other type.
The indexing operators are, in order of generality:
expression [ expression2 ]
expression . identifier
expression : identifier ( parameter-list )
The latter two are just "syntactic sugar" and can be rewritten in the form of any above it.
You would use the second if "expression2" would always the same string that is a valid Lua identifier and you want to hardcode it.
You would use the third if the value returned by indexing "identifier" against "expression" will always be a function that you want to call with the value returned by "expression" as an implicit first parameter. Such a function call is called a "method call."
Also, note that the language/compiler doesn't care/know if the field value is a function or not (you'd get an error at runtime if you try to call a value that isn't a function). Nor does it care/know if the function is a method or not (the function probably won't behave as you intended if you don't pass it appropriate parameters).
Now, the type of the expression value must be any type that can be indexed. Note that expressions don't have compile-time types so if the expression value is of a type that cannot be indexed, that is a runtime error, too. Indexable types are: table and any object with an __index metamethod. Other answers provide details on these.

Meta metatables?

I'm trying to make an __index function in my table which can process ALL of the field it receives.. What I want to do is that if I call the table in the following way
mytable.str1.str2.str3
I should be able to return the table
{"str1", "str2", "str3"}
Note that str1,str2,str3 are undefined, they are just strings. I am not trying to create subtables str1, str2, I just want __index to see everything beyond the first period.
Unfortunately what I have seems that __index only captures str1, and complains that "attempt to index field 'str1' (a nil value)"
Anyone know how this can be done?
I'm not sure why you'd want to do this, but here's how you do it. The comments explain the trick, but basically you need a second metatable to handle the table that's returned from the first call to the __index metamethod.
If this isn't clear, let me know and I can explain in more detail.
-- This metatable appends the new key to itself, then returns itself
stringtablemeta = {}
function stringtablemeta.__index(self, key)
table.insert(self, key)
return self
end
-- In response to the question in the comments:
function stringtablemeta.__tostring(self)
local str = ""
for i, v in ipairs(self) do
if i > 1 then str = str .. "-" end
str = str .. v
end
return str
end
-- This metatable creates a new table, with stringmetatable as its metatable
mytablemeta = {}
function mytablemeta.__index(self, key)
local temp = { key }
setmetatable(temp, stringtablemeta)
return temp
end
-- set mytable to have mymetatable as it's metatable. This makes it so when
-- you index into it, it will call the mytablemeta.__index method.
--
-- That will return a talb with a single string, the key that was passed
-- in. that table will have it's own metatable, the stringmetatable, which
-- will cause it to append keys that are called on it with its own __index
-- metamethod
mytable = {}
setmetatable(mytable, mytablemeta)
test = mytable.str1.str2.str3
for k, v in pairs(test) do
print(k, v)
end
It can't. Not without having a metatable on each of those tables.
mytable is a table. str1 is a different table. So you can do the same thing by doing this:
local temp = mytable.str1
temp.str2.str3
And as far as Lua is concerned, these are equivalent. Therefore, the only way to know what was done at each stage is to give all of them a special metatable. How you concatenate the different values into a table is something you'll have to investigate on your own.
As Nicol said, you cannot do that directly in Lua. However, by returning specially crafted tables, you can achieve a similar result to what you want. Take a look at AutomagicTables at the Lua-users Wiki for inspiration.

Resources