View Single Post
04-17-14, 05:35 AM   #9
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Thanks everyone for your advice.

Originally Posted by semlar View Post
This is not correct. You can compare self.TestFunction == TestFunction in your example to see that they are references to the same function.

It sounds to me like however you were redefining the function in your test was creating a new instance of it.
Yeah Semlar, I noticed that too after I'd written that post...

Lua Code:
  1. local function func() end
  2. local self = CreateFrame();
  3. self.func = func;
  4. print( self.func == func ); -- which returned true.

That would indicate that self.func and func both point to the same instance of the same function.

But it confuses me a little. Here's my example (and what made me think that it's actually a different instance of the same function:

Lua Code:
  1. local t, frame = {}, {};
  2. function t.func()
  3.   print("a");
  4. end
  5.  
  6. frame.func = t.func;
  7.  
  8. frame:func() -- outputs 'a'
  9.  
  10. t.func = nil; -- the original function no longer exists
  11.  
  12. frame:func() -- still outputs 'a'

So surely frame.func doesn't simply point to t.func because if that were the case, it would return an error. Wouldn't that indicate that each frame the method is added to will hold it's own instance of that method?

Just to be clear, I'm not in any way trying to insist that what I'm saying is right... to be honest, I'm stumped.

I'd always thought that one of the primary benefits of a metatable is to allow numerous objects to refer to the same instance of a method, thereby conserving memory.

If I'm looking at this all wrong, please let me know.
__________________
__________________

Last edited by Aanson : 04-17-14 at 05:46 AM.
  Reply With Quote