Questions tagged [metatable]

Typically it refers to a feature of Lua which allows a programmer to change the behaviour of tables. The meaning may be dependant on other tags used with it.

In Lua a metatable is used to contain metadata or metafunctions for any Lua value.

There are certain fields within a metatable which (if they exist) have specific responsibilities when used with operators. These are __add, __sub, __mul, __div, __mod, __pow, __unm, __concat, __len, __eq, __lt, __le, __index, __newindex, and __call.

A metatable can have its own metatable and this is known as metatable chaining.

For more information see the Lua reference manual. For a detailed description about meta-tables and meta-methods, see PiL (Programming in Lua). A few of metamethods available are:

  1. Arithmetic Metamethods
  2. Relational Metamethods
  3. Library-Defined Metamethods
  4. Table-Access Metamethods

    • The __index Metamethod
    • The __newindex Metamethod
    • Tables with Default Values
    • Tracking accesses
181 questions
8
votes
1 answer

Does next() look for a __pairs metamethod?

In general, the syntax: for k, v in pairs(t) do .... end is equivalent to: for k, v in next, t do .... end But what if t has a __pairs metamethod? Will the standard next() function check for this? If not, isn't it better to always use…
Siler
  • 8,976
  • 11
  • 64
  • 124
7
votes
2 answers

what is actual implementation of lua __pairs?

Does anybody know actual implementation of lua 5.2. metamethod __pairs? In other words, how do I implement __pairs as a metamethod in a metatable so that it works exactly same with pairs()? I need to override __pairs and want to skip some dummy…
user2872907
  • 71
  • 1
  • 2
6
votes
1 answer

How to get data from table in Lua

I have a table: Table = { button = {}, window = {}, label = {}, edit = {}, error = {} } How I can get keys and values of the table? I tried to get as: for key, value in ipairs(Table) do for k, v in ipairs(key) do …
owl
  • 4,201
  • 3
  • 22
  • 28
6
votes
1 answer

Lua Metatable Inconsistency

I'm having trouble understanding why there is a difference in behavior of the __index metamethod between these to examples: A = { __index = A } function A:speak() print("I'm an A") end An_A = setmetatable({},A) An_A:speak() Will raise the…
HennyH
  • 7,794
  • 2
  • 29
  • 39
5
votes
1 answer

Can Lua support case-insensitive method calls?

I'm using Lua as a data description language for my C++ app. I have a bunch of C++ classes bound to Lua using SLB 2.0. I have methods bound such as 'SetPos' or 'SetName'. I specify the position or name (for example) using a table with values…
5
votes
2 answers

Lua - understanding setmetatable

I am trying to build a CNN using Torch 7. I am very new to Lua. I was trying to follow this link. I encountered something called setmetatable in the following code block: setmetatable(train_set, { __index = function(t, i) return {t.data[i],…
skr
  • 914
  • 3
  • 18
  • 35
5
votes
1 answer

Lua String Append

So I created a function that all strings can use and it's called append. local strmt = getmetatable("") function strmt.__index.append(self, str) self = self..str return self end The function is then used like this: self =…
Lee Yi
  • 517
  • 6
  • 17
5
votes
1 answer

What is newproxy and how is it useful?

I was messing around with Lua yesterday and stumbled upon the 'newproxy' function. http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#newproxy I kind of understand it, but I'm not sure how it is useful. I know it creates a blank…
David
  • 693
  • 1
  • 7
  • 20
5
votes
1 answer

Lua table length function override not working

How does one change the length operator (#) for a table in Lua, the manual suggests assigning the __len function in a metatable and then assigning that metatable to the table I want to override, but this doesn't work as expected? I do not have the…
fisherdog1
  • 65
  • 5
5
votes
2 answers

In lua is there a way to bind an upvalue to a userdata value instead of a function?

In the following example a userdata value is created of type MyType and a table is created with a metafunction __tostring which calls LI_MyType__tostring. The code creates a closure-based lua OOP. My gripe with the example provided is it appears as…
Sean
  • 9,888
  • 4
  • 40
  • 43
5
votes
1 answer

Lua C API - Getting metatable from a table on stack

Let's say we have a table that was passed to a function and it's now on top of the stack like so: // -1 = table Is it possible to get the metatable from that table on stack? I can simply get it with a known-name identifier like…
Grapes
  • 2,473
  • 3
  • 26
  • 42
5
votes
2 answers

I need clarification on Metatable.__index

I asked earlier why my methods for a metatable weren't being located by Lua, and was told that by setting __index to my metatable, that it would resolve the issue, so I assumed that a method when called was searching by index in the metatable, but…
Weeve Ferrelaine
  • 683
  • 1
  • 7
  • 12
5
votes
3 answers

Lua, c++ and disappearing metatables

Background I work with Watusimoto on the game Bitfighter. We use a variation of LuaWrapper to connect our c++ objects with Lua objects in the game. We also use a variation of Lua called lua-vec to speed up vector operations. We have been working…
raptor
  • 799
  • 1
  • 5
  • 16
4
votes
1 answer

Why in lua, whith metavalue __index, t = {_index} is nil?

When, in lua, I run this code, the metavalue __index is nil t = {__index = t} print(t.__index) --prints nil but if I write it like this... t = {} t.__index = t print(t.__index) --prints table **** ... it works My question is why.
SpaceChaton
  • 145
  • 7
4
votes
1 answer

why can you set __index equal to a table

The index metamethod can be set equal to tables. From what I can tell foo.__index = function(self, k) return bar[k] end and foo.__index = bar are the same. Why is declaring functions this way allowed in this situation?
Ace shinigami
  • 1,374
  • 2
  • 12
  • 25
1
2 3
12 13