Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "table:insert()" do?

I understand what colon syntax does. I know what table.insert(list, value) does. I'm also aware that I cannot create my own table t={} and insert a value to it with t:insert(value). But when I do table:insert(value) it inserts the value to table which is supposed to a type, right? The worst thing is that I can read this value by calling table[1]. What did I just do? How did I insert a value into a type? Why regular tables don't support colon syntax? I tried to Google it up but I just get information about tables in general, not about this particular case.

like image 816
Sun of A beach Avatar asked Nov 19 '25 17:11

Sun of A beach


2 Answers

What did I just do?

The syntax A:B(C) is nearly equivalent to A.B(A, C), you can check my another answer: link.

So table:insert(value) just means table.insert(table, value), it inserts value into the table table (have nothing to do with t).

How did I insert a value into a type?

table.insert(t, value)

Why regular tables don't support colon syntax?

Because t:insert(value) means t.insert(t, value), but this regular table doesn't have the key insert.

You can solve this by adding the function to the table

t.insert = table.insert

Or using a metatable

setmetatable(t, {__index = {insert = table.insert}})
like image 175
shingo Avatar answered Nov 21 '25 08:11

shingo


In Lua the only datatypes where you can use the colon directly for assigned datatype specific methods are string and userdata
But its easy to set the table functions as methods on self created tables...

> myTable = setmetatable({}, {__index = table})

Than you can do...

> myTable:insert("Hi")
> myTable:insert("There")
> print(myTable:concat(" "))
Hi There

But chaining the table methods like the most string methods is not possible.
Because for chaining the method has to return what the next method can work with (as argument).
Like...

> print(("Hi there"):upper():rep(10, ", ")) -- Chaining methods on datatype string
HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE

But you can chain what myTable:concat() will return with string methods...

> print(myTable:concat(" "):rep(10, ", "):upper())
HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE, HI THERE

Because myTable:concat() returns a string.

PS: Thinking about datatype specific methods and deciding to add math functions for datatype number as methods
That will be done with...

> debug.setmetatable(1, {__index = math})

Than math and calculating with datatype number becomes some magic...

> print((0.0).pi) -- Any number has pi
3.1415926535898
> print((1.1).pi:deg()) -- Convert pi to degrees
180.0
> print((180):rad()) -- Convert 180 degrees to radians
3.1415926535898
> print(("%a"):format((1).pi)) -- Pi to HEX float (string)
0x1.921fb54442d18p+1
> print((0x1.921fb54442d18p+1):deg()) -- HEX float to degrees
180.0
like image 23
koyaanisqatsi Avatar answered Nov 21 '25 09:11

koyaanisqatsi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!