WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   metatables help to understand? (https://www.wowinterface.com/forums/showthread.php?t=57145)

urakkaamyx 05-14-19 09:51 PM

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 = {}
  2.  
  3. metatable = {}
  4. setmetatable(SomeClass, metatable)
  5.  
  6. function SomeClass:DoSomething ( table, ... )
  7.     return "Something Happens"
  8. end
  9.  
  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)

Vlad 05-15-19 06:56 AM

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

SDPhantom 05-15-19 04:42 PM

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.



Quote:

Originally Posted by Vlad (Post 332091)
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.
http://www.lua.org/manual/5.1/manual.html#2.8

urakkaamyx 05-15-19 07:49 PM

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.


All times are GMT -6. The time now is 12:23 AM.

vBulletin © 2019, Jelsoft Enterprises Ltd
© 2004 - 2019 MMOUI