Thread: Window Chaining
View Single Post
03/07/14, 05:19 AM   #5
SinusPi
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 18
Let me clarify :)

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.

Originally Posted by Lodur
Right in that if every method on the control did return self then chaining would not be needed.
Rather - it'd be chainable by default (like jQuery is). it's extra chain-wrapping that would not be needed.


Glad to see my little tangle of code get such attention, have fun with it


Oh, one thing that might be interesting - I've had it asked "why doesn't the __index function (self,func) just call the relevant object's function and return the object as results, but instead a whole new function is made and returned? isn't it wasteful?" - well, it IS wasteful. But an __index call doesn't get any parameters that the "methods" need to be called with. Outside code wants to get a function, with Something.Method, not function call results, so it needs to be given a function to call, even if "fake".

Consider this:
Code:
object:method("param")
is the same as
Code:
 m = objec*****thod
 m (object,"param")
So, it first accesses "method" in "object"...
... so if "object" has no "method", just a metatable with __index in it, then __index(object,"method") is called, and the result is returned into m.
At this point it needs to be a function, that can get called with (object,"param").

I wonder if I made it clearer or more tangled... :>

Last edited by SinusPi : 03/07/14 at 06:59 AM.
  Reply With Quote