1
function string.test(s)
    print('test')
end

a = 'bar'
string.test(a)
a:test()

All is fine until next example.

function table.test(s)
    print('test')
end

b = {1,2,3}
table.test(b)
b:test() -- error

Why do I getting the error?
It was working fine at strings.

Parki
  • 159
  • 9
  • 1
    Possible duplicate of [How do I add a method to the table type?](http://stackoverflow.com/questions/10778812/how-do-i-add-a-method-to-the-table-type) – Tom Blodget Nov 30 '15 at 03:18

2 Answers2

5

Tables do not have a metatable by default like strings do.

Try instead:

function table.test(s)
    print('test')
end

b = setmetatable({1,2,3}, {__index=table})
table.test(b)
b:test() -- error
daurnimator
  • 4,091
  • 18
  • 34
2

While daurn answers your question nicely, allow be to explain why this is.

In Lua, all datatypes can have a metatable. (Although it's quite different when dealing with numbers,bools,etc. See debug.setmetatable.) This includes the string. By default this is set to index string library with __index, it is when make syntactic sugar like print(s:sub(1,5)) (s being a string) possible.

This is NOT the same with tables. By default tables have NO metatable. You have to manually set it with setmetatable.

And to conclude my answer, enjoy this code snippet

debug.setmetatable(0,{__index=math})
debug.setmetatable(function()end,{__index=coroutine})
debug.setmetatable(coroutine.create(function()end), {__index=coroutine})
local function Tab(tab)
    return setmetatable(tab,{__index=table})
end

This basically allows you to use math functions on numbers

local x = 5.7
print(x:floor())

And do similar things with functions and threads, using coroutine functions:

print:create():resume()

Pretty hacky if you ask me

And of course, create tables and use table functions on them:

local x = Tab{1,2,3,4}
x:insert(5)
x:remove(1)
print(x:concat(", "))

I find it hard to imagine anybody who don't like cool tricks like that.

warspyking
  • 3,045
  • 4
  • 20
  • 37
  • I copypasted your code and for a reason I got a error "attempt to index local 'x' (a number value)" I'm using Lua 5.1.4 on windows 7. – Parki Nov 30 '15 at 10:55
  • I doubt that it works in Lua 5.2. It should be `debug.setmetatable(0, { __index=math })`. (And that works on Lua 5.1 as well.) – siffiejoe Dec 01 '15 at 03:30
  • @siffiejoe So it is. Thanks, I don't know how I pasted the wrong code. I blame iOS! – warspyking Dec 01 '15 at 21:03
  • @Parki It has been fixed. – warspyking Dec 01 '15 at 21:04
  • @warspyking is it possible to set a metatable to every table by defualt like in your "Tab" function? – Parki Dec 01 '15 at 21:21
  • @Parki While there's no way to detect when an anonymous table (I think you can call them that), you can set a metatable to _G and wrap it so that new tables assigned to global variables have that metatable by default. Out of curiosity though, why not be consistent and put your function in table and call it like any other... `table.test(tab)` – warspyking Dec 02 '15 at 03:00
  • @warspyking improved your code a bit https://github.com/Parki0/LuaColonSuntaxSugar – Parki Jan 19 '16 at 13:11
  • @Parki Well obviously you can make a constructor function. You said by default though. – warspyking Jan 19 '16 at 13:29