I don't understand this application of metamethod

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

I don't understand this application of metamethod

Emeka

What is the essence of the below?

local Request = {}
Request.__index = Request

Regards, \Emeka
--
Satajanus  Nig. Ltd



Reply | Threaded
Open this post in threaded view
|

Re: I don't understand this application of metamethod

Tim Mensch
On 5/24/2012 10:25 PM, Emeka wrote:
> What is the essence of the below?
>
> local Request = {}
> Request.__index = Request

The above doesn't do anything interesting by itself, though if Request is later assigned as the metatable for another table, AND the Request table had another member, it would be relevant:

local aRequest = {}
setmetatable( aRequest, Request ); -- Request is now the metatable of the table referred to by "aRequest"

function Request:someFunction()
    print("a function on Request")
end

aRequest:someFunction()

The __index metamethod tells Lua what to do if someone tries to look up an undefined key on a table. If __index is a function, it gets called. If __index is a table, then it looks for the key in that table. So when Lua can't find a member on aRequest, it tries the __index metamethod, sees that it's a table, finds the key "someFunction" on that table, and then tries to call it as a function.

Tim
Reply | Threaded
Open this post in threaded view
|

Re: I don't understand this application of metamethod

Emeka
Timi de lua,


Thanks so much!!!

On Fri, May 25, 2012 at 7:13 AM, Tim Mensch <[hidden email]> wrote:
On 5/24/2012 10:25 PM, Emeka wrote:
> What is the essence of the below?
>
> local Request = {}
> Request.__index = Request

The above doesn't do anything interesting by itself, though if Request is later assigned as the metatable for another table, AND the Request table had another member, it would be relevant:

local aRequest = {}
setmetatable( aRequest, Request ); -- Request is now the metatable of the table referred to by "aRequest"

function Request:someFunction()
    print("a function on Request")
end

aRequest:someFunction()

The __index metamethod tells Lua what to do if someone tries to look up an undefined key on a table. If __index is a function, it gets called. If __index is a table, then it looks for the key in that table. So when Lua can't find a member on aRequest, it tries the __index metamethod, sees that it's a table, finds the key "someFunction" on that table, and then tries to call it as a function.

Tim



--
Satajanus  Nig. Ltd



Reply | Threaded
Open this post in threaded view
|

Re: I don't understand this application of metamethod

Dirk Laurie-2
In reply to this post by Tim Mensch
2012/5/25 Tim Mensch <[hidden email]>:


> The __index metamethod tells Lua what to do if someone tries to look up an
> undefined key on a table.

And (ex-Pythonistas please beware) it does NOT override the
normal table indexing method when the key is already present.

Reply | Threaded
Open this post in threaded view
|

Re: I don't understand this application of metamethod

Emeka
Dirk,

If won't mind, I would like to see an example.

Regards, Janus

On Fri, May 25, 2012 at 8:10 AM, Dirk Laurie <[hidden email]> wrote:
2012/5/25 Tim Mensch <[hidden email]>:


> The __index metamethod tells Lua what to do if someone tries to look up an
> undefined key on a table.

And (ex-Pythonistas please beware) it does NOT override the
normal table indexing method when the key is already present.




--
Satajanus  Nig. Ltd



Reply | Threaded
Open this post in threaded view
|

Re: I don't understand this application of metamethod

Rob Kendrick-2
On Fri, May 25, 2012 at 10:53:06AM +0100, Emeka wrote:
> Dirk,
>
> If won't mind, I would like to see an example.

There's no need for one; Dirk's basically explained everything about it
in his one sentence.  Things that aren't in a table get looked up via
__index.

B.

Reply | Threaded
Open this post in threaded view
|

Re: I don't understand this application of metamethod

Rena
In reply to this post by Emeka
On Fri, May 25, 2012 at 3:53 AM, Emeka <[hidden email]> wrote:

> Dirk,
>
> If won't mind, I would like to see an example.
>
> Regards, Janus
>
> On Fri, May 25, 2012 at 8:10 AM, Dirk Laurie <[hidden email]> wrote:
>>
>> 2012/5/25 Tim Mensch <[hidden email]>:
>>
>>
>> > The __index metamethod tells Lua what to do if someone tries to look up
>> > an
>> > undefined key on a table.
>>
>> And (ex-Pythonistas please beware) it does NOT override the
>> normal table indexing method when the key is already present.
>>
>
>
>
> --
> Satajanus  Nig. Ltd
>
>
>

Well, to show an example of using __index:

local meta = {}
local methods = {}
meta.__index = methods
function method:getContent() return self.content end

local myObject = setmetatable({content='hello'}, meta)
print(myObject:getContent()) --prints hello

What you posted about is the same idea, except the metatable points to itself:

local meta = {}
meta.__index = meta
function meta:getContent() return self.content end

local myObject = setmetatable({content='hello'}, meta)
print(myObject:getContent()) --prints hello

The only major difference doing it this way is that now
myObject.__index returns the metatable, since it's now looking in
'meta', instead of 'methods' where no such key exists.

--
Sent from my Game Boy.

Reply | Threaded
Open this post in threaded view
|

Re: I don't understand this application of metamethod

Emeka
In reply to this post by Rob Kendrick-2
Rob,

That's the basic work on __index.. missing method close cousin. 

On Fri, May 25, 2012 at 10:55 AM, Rob Kendrick <[hidden email]> wrote:
On Fri, May 25, 2012 at 10:53:06AM +0100, Emeka wrote:
> Dirk,
>
> If won't mind, I would like to see an example.

There's no need for one; Dirk's basically explained everything about it
in his one sentence.  Things that aren't in a table get looked up via
__index.

B.




--
Satajanus  Nig. Ltd



Reply | Threaded
Open this post in threaded view
|

Re: I don't understand this application of metamethod

Emeka
In reply to this post by Rena
Rena,

Thanks so much.

local meta = {}
local methods = {}
meta.__index = methods
function method:getContent() return self.content end
function methods:getContent() return self.content end  -- method to methods

local myObject = setmetatable({content='hello'}, meta)
print(myObject:getContent()) --prints hello


On Fri, May 25, 2012 at 11:01 AM, Rena <[hidden email]> wrote:
On Fri, May 25, 2012 at 3:53 AM, Emeka <[hidden email]> wrote:
> Dirk,
>
> If won't mind, I would like to see an example.
>
> Regards, Janus
>
> On Fri, May 25, 2012 at 8:10 AM, Dirk Laurie <[hidden email]> wrote:
>>
>> 2012/5/25 Tim Mensch <[hidden email]>:
>>
>>
>> > The __index metamethod tells Lua what to do if someone tries to look up
>> > an
>> > undefined key on a table.
>>
>> And (ex-Pythonistas please beware) it does NOT override the
>> normal table indexing method when the key is already present.
>>
>
>
>
> --
> Satajanus  Nig. Ltd
>
>
>

Well, to show an example of using __index:

local meta = {}
local methods = {}
meta.__index = methods
function method:getContent() return self.content end

local myObject = setmetatable({content='hello'}, meta)
print(myObject:getContent()) --prints hello

What you posted about is the same idea, except the metatable points to itself:

local meta = {}
meta.__index = meta
function meta:getContent() return self.content end

local myObject = setmetatable({content='hello'}, meta)
print(myObject:getContent()) --prints hello

The only major difference doing it this way is that now
myObject.__index returns the metatable, since it's now looking in
'meta', instead of 'methods' where no such key exists.

--
Sent from my Game Boy.




--
Satajanus  Nig. Ltd