Thread: Window Chaining
View Single Post
03/07/14, 10:19 AM   #7
inDef
Join Date: Mar 2014
Posts: 16
Originally Posted by SinusPi View Post
I'm the author of the original CHAIN function, harkening back to my WoW "Spoo" addon - let me clarify it for you

It IS a pretty tricky thing, I give you that. But, InDef made a pretty good job at analyzing it!

In layman's terms, all the CHAIN does is make a "wrapper" for the table ("object") it receives. The wrapper is a table T (with a metatable on it), whose sole function is to intercept direct calls to the object's functions and force each function call to return the wrapper again, instead of the usual return values, so that more and more calls can be made into it.

Ultimately, the __BALL (or simply __END as it was originally) is intercepted to return the object itself, as otherwise we'd be left with the wrapper.


Rather - it'd be chainable by default (like jQuery is). it's extra chain-wrapping that would not be needed.
Ok I think I understand. The chain "wrapper" forces any method called as part of the chain to return "self" as the object. This allows us to call many methods on a particular object, without having to modify those functions to return "self" so the chain can continue.

I'm still curious though, it sounds like then that "self" throughout the whole process is the original returned table T. So each step along the chain we're defining new "functions" that will essentially execute the methods (like SetHidden) and then return "self" which if I'm understanding correctly is T. If we are constantly returning functions, at what point do they get executed?

For example..in the "Programming in Lua" manual in the "Closures" section, we see the following example:

Code:
    function newCounter ()
      local i = 0
      return function ()   -- anonymous function
               i = i + 1
               return i
             end
    end
    
    c1 = newCounter()
    print(c1())  --> 1
    print(c1())  --> 2
From this example we can see that when c1 is created, the returned function is not actually executed. The function isn't executed for the first time until c1() is evaluated as the parameter to the print() statement.

So going back to the CHAIN function, when exactly do all of the returned functions in the chain get executed?
  Reply With Quote