sorry this is more of a pure LUA question but it is being used in my game so...
anyway, i really dont understand what __index does. could someone explain in layman's terms - the lua website isnt helping. they've made it very confusing. the example they've given is found here: http://lua-users.org/wiki/MetamethodsTutorial (scroll down a little bit bellow String class example). i sort of understand what that does but i dont think it's quiet it. to me it sounds like its just adding the length function to the mt metatable. and since s has had its metatable set to mt earlier on, it can now use this function. am i correct? is this the only use for it..? :
__index, i really dont get it
Re: __index, i really dont get it
Okay, here's the short version:
__index is a special field used in a metatable. It defines a function which allows you to define what happens when you attempt to access an index of a table that doesn't exist.
And here's an example (outside of the String one):
Say you wanted to create a NumberTable which acts like a regular Lua table but can only have numbers in it.
It starts out simply enough:
Now, normally when you look up a non-existent index of a table, it returns nil. But this is a number table, it can only have numbers in it. So instead of returning nil, let's have it return 0.
And this __index field in action:
So, the __index field is very flexible, and can be used for all sorts of things to make tables do things they normally wouldn't be able to do.
Does it make more sense now?
EDIT: And here's what they're doing in the String example:
Lua's string library has a built-in function called string:len() which returns the length of a string. What the wiki is doing with the String class is making it so if you access the "length" index of a String table (an index which doesn't actually exist!), instead of returning nil, it calculates the length of the string stored in String.value and returns that. So, it's like the "length" index actually exists, even though it doesn't. But it will automatically update if you change the string stored in String.value, since String.length is not actually stored in memory as a number, just calculated when you try to access it.
__index is a special field used in a metatable. It defines a function which allows you to define what happens when you attempt to access an index of a table that doesn't exist.
And here's an example (outside of the String one):
Say you wanted to create a NumberTable which acts like a regular Lua table but can only have numbers in it.
It starts out simply enough:
Code: Select all
NumberTable = {} -- create our table
mt = {} -- create our metatable
setmetatable(NumberTable, mt)
Code: Select all
mt.__index = function(t, key)
return 0
end
Code: Select all
NumberTable.foo = 42
print(NumberTable.foo)
> 42
print(NumberTable.bar)
> 0
Does it make more sense now?
EDIT: And here's what they're doing in the String example:
Lua's string library has a built-in function called string:len() which returns the length of a string. What the wiki is doing with the String class is making it so if you access the "length" index of a String table (an index which doesn't actually exist!), instead of returning nil, it calculates the length of the string stored in String.value and returns that. So, it's like the "length" index actually exists, even though it doesn't. But it will automatically update if you change the string stored in String.value, since String.length is not actually stored in memory as a number, just calculated when you try to access it.
Re: __index, i really dont get it
thanks that makes alot more sense . also, ive looked up examples on metatables; to me they appear like they're simply a collection of functions that we can associate a table with, right?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: __index, i really dont get it
That's the gist of it. Another powerful metamethod is __newindex, which is called if you set a nonexisting table entry.Pliskin09 wrote:to me they appear like they're simply a collection of functions that we can associate a table with, right?
For example, keeping the table and metatable anjo used:
Code: Select all
mt.__newindex = function(t, key, value)
if type(value) ~= "number" then
error("Only numbers are allowed in NumberTables!")
else
rawset(t, key, value)
end
end
Help us help you: attach a .love.
Re: __index, i really dont get it
thanks robin and anjo, you guys are awesome!
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests