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
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)
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
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.
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 02:09 PM.|
vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2019 MMOUI