metatables help to understand? - WoWInterface
Thread Tools Display Modes
05-14-19, 09:51 PM   #1
A Murloc Raider
Join Date: May 2019
Posts: 8
metatables help to understand?

So i've been looking into meta tables, but i feel like use it would create more unnecessary code...
...unless i'm looking at it wrong.

So in the example i'm showing i'm wandering if i'm using it correctly.

can someone give a brief run down on why you need -or- should use metatables

Lua Code:
  1. local SomeClass = {}
  3. metatable = {}
  4. setmetatable(SomeClass, metatable)
  6. function SomeClass:DoSomething ( table, ... )
  7.     return "Something Happens"
  8. end
  10. function metatable:__call( table, ... )  
  11.     return SomeClass:DoSomething(table, ... )
  12. end

this being like this, i can just print(SomeClass()) and it would result in "Something Happens" ... buut I fell like itd be simplier to just do a static call print(SomeClass: DoSomething()) ... Space in ther to prevent emoticon and static may be the wrong term (php dev background)

Last edited by urakkaamyx : 05-14-19 at 10:03 PM. Reason: added more explaination
  Reply With Quote
05-15-19, 06:56 AM   #2
A Molten Giant
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 792
You can use metatable to achieve inheritance, which in return can be used to create some sort of class hierarchy even when lua itself doesn't have the syntax or concept of class or inheritance, you can manipulate a table to behave vastly different than a regular table when using metatables.

It really depends on how you implement it, but most cases such metatables provide some sort of syntactic sugar so to speak. Like you mentioned yourself you could in your example just call a function on the table instead of using metatable to define a custom _call function and so on.

The last point worth mentioning is that even if via metatables you can achieve a lot of funky behavior, in the end I recommend to consider what if someone else read this code, will they understand what it does?

One way you can use metatables to make it easier to read and maybe easier to update your code if you use it a lot of places, is to for example make a factory that produces and returns you a 3d vector representation, in lua it would be a table. You could use metatable on this to add basic math operations and code in how it would then modify the table given your input. It would make it clean to write local newvector = vector1 + vector2 and you would know to modify the metatable in your definition file for vectors if you want to expand functionality.

Then again you could also approach this and simply do local newvector = vector.add(vector1, vector2) where you have a table vector with all the operations that you need in it.

TLDR; metatables add syntactic sugar but are not mandatory in any way.

Just a link to the official documentation. https://www.lua.org/pil/13.html
Profile: Curse | Wowhead
  Reply With Quote
05-15-19, 04:42 PM   #3
A Pyroguard Emberseer
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,960
Essentially, what metatables do is allow you to define how a table reacts to different operations and even tag on some default values. The true power of metatables is an advanced topic. Many only use them to build structures similar to classes, but you can use them to hook/filter content, tweak how garbage collection works on the table and/or its contents, define what happens in math and comparison operations, and many more.

Originally Posted by Vlad View Post
Just a link to the official documentation. https://www.lua.org/pil/13.html
PiL is a good introduction to Lua. If you want more detail, I'd suggest the reference manual.
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
05-15-19, 07:49 PM   #4
A Murloc Raider
Join Date: May 2019
Posts: 8
Ahh yes, thank you both for replying,

Im glad i wasnt entirely wrong about them .... buut i did manage to learn a bit more about them after i posted. I think, Readablity wise... metatables are programming voodoo.. much like __magic functions in php. while yeah. useful, probably going to steer away from until i get a better understanding of lua.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » metatables help to understand?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off