Questions tagged [meta-method]

Anything related to Lua meta-methods (a.k.a. metamethods), i.e. functions that alter how a Lua table behaves in the context of some operations (e.g. math operations, element access, garbage collection, etc.). Meta-methods can be used to implement object oriented programming and other powerful techniques using Lua tables.

52 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
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
4
votes
1 answer

Is it possible to bypass __tostring the way rawget/set bypasses __index/__newindex in Lua?

For example: local my_table = { name = "my table" } local my_table_mt = {} function my_table_mt.__tostring(tbl) return "%s<%s>":format(tbl.name or "?", rawtostring(tbl)) end Is something like this possible? I know the rawtostring method…
Llamageddon
  • 3,306
  • 4
  • 25
  • 44
4
votes
3 answers

Metamethod when accessing a key as mutable

__index is called when accessing as immutable : local foo = bar["foo"]; __newindex is called when access as mutable an index that doesn't exist : local bar = { } bar["foo"] = 123 -- calls __newindex bar["foo"] = 456 -- does NOT call __newindex Is…
Virus721
  • 8,061
  • 12
  • 67
  • 123
4
votes
2 answers

Lua __metatable bypass?

I was wondering if there is any way to escape the __metatable metamethod. I know there isn't one, but I'm trying to do something like this, but obviously __metatable blocks that from happening: -- pretend that there is a __metatable field given…
4
votes
1 answer

How to fix this unpack issue?

I'm creating an Array class that adds more usage to tables. I have a metamethod that allows me to combine two tables, ex: Array(5) .. Array(6, 10) should give you {5, 6, 10} I'm aware that I can use two loops to do this, but I'm trying to make my…
David
  • 693
  • 1
  • 7
  • 20
4
votes
2 answers

Accessing deeply nested table without error?

For a field inside a deeply nested table, for example, text.title.1.font. Even if you use if text.title.1.font then ... end it would result in an error like "attempt to index global 'text' (a nil value)" if any level of the table does not actually…
Dionysian
  • 1,195
  • 2
  • 13
  • 24
4
votes
2 answers

How to save boolean conditions and evaluate later

I need to create a structure. The structure must contain an array of "boolean conditions". Something like this: function ReturnStructure () local structure = { {A < 10}, {B == "smth"}, …
4
votes
1 answer

modify # operator in lua

I have made a lua console on the love2d engine which is irrelevant. I am trying to upgrade the metatables to be able to effect more things (the pairs function, change the metatable on another table instead of the targeted one etc.) and one of the…
3
votes
2 answers

How to create a simple importable class in Lua?

I'd like to create MyClass class in Lua in a separate file myclass.lua which I can import and use later. It should be working the following way: local MyClass = require 'myclass' tab = {1,2,3} m = MyClass(tab) However, following the code in Lua…
minerals
  • 6,090
  • 17
  • 62
  • 107
3
votes
1 answer

Doesn't Lua inheritance include metamethods?

I'm trying to implement simple inheritance in Lua as it is presented in PIL 16.2. However, I've come across a surprising behavior: metamethods don't seem to be inherited. In the following example, I create a Point class with x and y members, and…
Ludwik
  • 2,247
  • 2
  • 27
  • 45
3
votes
1 answer

Lua C++ userdata matrix access to elements

I have a matrix class in C++ and the constructor is as follows: template CMatrix::CMatrix(unsigned int varrow,unsigned int varcolumn) { //Lets set member variables this->m_row=varrow;this->m_column=varcolumn; …
macroland
  • 973
  • 9
  • 27
3
votes
3 answers

How do we change the way print displays a table

Assuming I have a piece of code such as the following aTable = {aValue=1} aTable_mt = {} print(aTable) What must I do to make Lua print something like aTable current aValue = 1 as opposed to table: 0x01ab1d2. So far I've tried setting the…
No Name
  • 116
  • 7
3
votes
1 answer

Why does this cause a C stack overflow?

I'm aware that I can fix this problem by using rawset, but I'm just wondering why the following code causes a C stack overflow. local mt = { __newindex = function(self, key, value) self[key] = value end } local x =…
David
  • 693
  • 1
  • 7
  • 20
1
2 3 4