Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-07-2011, 12:54 PM
|
#1
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
Lua C API Question
Quote:
|
Originally Posted by http://www.lua.org/pil/28.3.html
Code:
static const struct luaL_reg arraylib_f [] = {
{"new", newarray},
{NULL, NULL}
};
static const struct luaL_reg arraylib_m [] = {
{"set", setarray},
{"get", getarray},
{"size", getsize},
{NULL, NULL}
};
The new version of luaopen_array, the function that opens the library, has to create the metatable, to assign it to its own __index field, to register all methods there, and to create and fill the array table:
Code:
int luaopen_array (lua_State *L) {
luaL_newmetatable(L, "LuaBook.array");
lua_pushstring(L, "__index");
lua_pushvalue(L, -2); /* pushes the metatable */
lua_settable(L, -3); /* metatable.__index = metatable */
luaL_openlib(L, NULL, arraylib_m, 0);
luaL_openlib(L, "array", arraylib_f, 0);
return 1;
}
|
I don't understand, what's the point of the "metatable.__index = metatable" part? It doesn't make any sense to me.
|
|
|
|
08-07-2011, 03:20 PM
|
#2
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
That part is just saying that it is taking metatable.__index and setting the value for that table. The = metatable just means that it is using the pointer of L; lua_settable(L, -3) is the actual code, whereas metatable.__index is just saying what is happening. Notice the /* */ around it... Sorry if it doesn't make sense, but that is the absolute best I can do to explain it at a lower level without getting into the pointer structure for L and how it is being set.
Cheers,
Josh
|
|
|
|
08-10-2011, 03:54 PM
|
#3
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Original Poster
|
I didn't know that anyone replied intil I tried looking for this thread again just in case. The list of unread threads on "My LQ" is so glitchy...
Quote:
Originally Posted by corp769
That part is just saying that it is taking metatable.__index and setting the value for that table. The = metatable just means that it is using the pointer of L; lua_settable(L, -3) is the actual code, whereas metatable.__index is just saying what is happening. Notice the /* */ around it... Sorry if it doesn't make sense, but that is the absolute best I can do to explain it at a lower level without getting into the pointer structure for L and how it is being set.
|
I understand what it does. I don't understand why wou would want to do it.
Why would you want to set the metatable's __index field to the metatable itself?
Last edited by MTK358; 08-10-2011 at 03:56 PM.
|
|
|
|
08-13-2011, 01:53 PM
|
#4
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Original Poster
|
Code:
Example = {}
-- if I comment the following line out, it fails to get methods using the ":"
-- syntax. Why is that?
Example.__index = Example
function Example.new(value)
self = {}
setmetatable(self, Example)
self:setvalue(value)
return self;
end
function Example:setvalue(value)
self.value = value
end
function Example:getvalue(value)
return self.value
end
i = Example.new(5)
print(i:getvalue())
See the comment in the above code.
|
|
|
|
08-14-2011, 02:06 PM
|
#5
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
Original Poster
|
It says that the __index metamethod (which can be a table instead of a function) is used when you try to get a table's member that doesn't exist. In that case, I would expect that setting the __index field of a table's metatable to the table's metatable would make it so that you can call methods using the "." operator instead of ":". But it doean't work that way. What am I missing here?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:22 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|