View Single Post
08-25-14, 03:12 PM   #19
Sharparam
A Flamescale Wyrmkin
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 102
One reason could be if you make something that you want to behave like a Frame's event/script system. Say you want custom callbacks for some object you have, like:

lua Code:
  1. local object = {callbacks={}}
  2.  
  3. function object:SetScript(typ, func)
  4.   if not self.callbacks[typ] then self.callbacks[typ] = {} end
  5.   table.insert(self.callbacks[typ], func)
  6. end
  7.  
  8. function object:Fire(typ, ...)
  9.   if not self.callbacks[typ] then return end
  10.   for _, f in pairs(self.callbacks[typ]) do f(...) end
  11. end
  12.  
  13. object:SetScript("MyCustomTrigger", function() print("foo bar baz") end)
  14.  
  15. -- Some code that does stuff and triggers the callbacks
  16. object:Fire("MyCustomTrigger")

Yes, there's CallbackHandler lib for this kind of stuff, but sometimes you just want to roll your own when you don't utilize near half the features of an existing library or don't like the particular way they implemented things.

Implementing the above on a Frame wouldn't really work while making sure there aren't any problems with the Frame's own SetScript and related types of events/triggers. Forwards compatibility can also be a concern, why put your addon at risk of being made incompatible with a future patch when it's preventable with little effort? Using different names would work, but why not choose a method that lets you use whatever name you feel fits best.

And if you're just blindly using a Frame to put all your methods in, you could be unknowingly replacing something that the Frame actually uses for whatever purpose if you don't check the documentation before implementing a method.

I like to have the comfort of knowing that whatever I'm putting in my tables, won't directly conflict with anything and lives in its own space. I do sometimes put things in Frames, but mostly for one-off objects. An example being in the options file of one of my addons (code is in MoonScript, but should be relatively readable I hope).
  Reply With Quote