The useful, yet strange world of metatables

:man_shrugging: Why should we bother even using metatables?

This really comes down to your preference; whether you prefer readability, maintainability, performance, etc. However, metatables can really help you do repetitive tasks along with a more syntatic sugar feel. (in my opinion)

Metatables can be substituted with regular functions, but with the downside of having to call it manually.

Metamethods can mimic the behavior of getters and setters (specifically the __index and __newindex metamethod)

:construction: â €This topic is still a work-in-progress.

There shall be more metamethod applications added in the future. If there’s any mistakes I made, please correct me as I am not an expert at this. I hope that this topic can help people understand the usage of metatables and its incredible versatility.

:balance_scale: Pros and cons of using metatables

  • Pros:
    • Convenience
    • Versatility
    • Capabilities
  • Cons:
    • Memory & Performance overhead
    • Complexity
    • Maintenance
    • Ambiguity

:open_book: Useful metamethods applications

__index (getter)

The most frequent use for this is to set __index to another table to be able to “combine” the two tables.

However, It also can be set to a function. In which the function will be fired whenever an Index is not found.

The code above is an example of using __index to program a getter behavior. In which it doesn’t really exists, but when you index a non-existent key; It will run the function every single time

Which is a beneficial behavior to have! If you have a constantly changing reference value that you need to track off, you’d need to update the value to sync correctly with the reference value. But with __index, you can just let it come to life!

Here is another example, with slightly more values.


The code above has an Costs table, programmed to have getters of G, H, and F. These values are frequently used in an A* algorithm, so it is good to have them easy to access.

In the __index metamethod, you can index another non-existent value, which will run another instance of the function. However, be careful using this, as you this can lead to a C-Stack overflow. To bypass the __index metamethod, you can use rawget.

__newindex (key-freezing)

The __newindex is similar to __index, but the only difference being __newindex fires whenever a new key/index is added. You can use this to do stuff such as key freezing

The code above “freezes” the key “c”, making it immutable. It’s important to do rawset(t, k, v) instead of t[k] = v inside of the __newindex metamethod. If we did t[k] = v, that’ll cause another C-Stack overflow. So to bypass the __newindex metamethod, we can use rawset

10 Likes

If you dont mind what VSCode theme is that?

also whats the font name?

1 Like

Its Ayu Dark, somewhat easy on the eyes

I’m not sure where to get my font or change it
found it: Consolas, 'Courier New', monospace

For the snippets, i use the CodeSnap extension

1 Like

Hey, I suggest You may use Fire Code (GitHub - tonsky/FiraCode: Free monospaced font with programming ligatures), it improves the coding experience overall!

1 Like

Sorry for bumping but,i still dont see the reason to use metatables,not only are they so confusing to learn,there is no real tutorial showing a example of where they can be used or how should we use them in whatever situations.

1 Like

Lua doesn’t really adheres to a specific programming paradigm, and metatables help making it much more flexible for users to implement their own paradigm. Metatables is like going on a deeper and lower level of the language, while it certainly looks overly complicated and somewhat of a “bloat”, it enables users to go to a lower level without going too far off, remember, Lua is an embeddable language, its used widely across the world. Whether you find it useless or not, the tool is right there for you to use, or not.

This topic was made with the sole purpose of showing how metatables can be used, as of right now I’ve provided 3 scenario examples, the most common being __index, which can be used to get the player’s leaderstats value

tl;dr metatables allow you to modify certain inner workings of a table