There’s a special place in hell reserved for whoever invented metatables.
I’ve just spent the best part of two days tugging at my flaxen locks trying to work out why the table returned by the __index of a metatable was being updated by indirect access instead of throwing an ‘attempt to index nil’ error for the public table when setting a value like this:
-- this table has no value for index (nil), but a metatable
-- with an '__index' method to return a default value
table_with_metatable[index].value = something
-- expect that it would complain of no field 'value', but instead
-- sets the metatable[index][value] variable to 'something'
So, it’s fetching the table at __index and then the dot operator is just sugar for [ ] on the table that lives at the index of the metatable. How infuriating. It makes somewhat more sense as i write it out here, but maybe i’m just stupid and this is obvious to everyone else. Zeus strike me down if i don’t find C++ references and pointers much easier to deal with. Just give us a ‘read only’ flag to set already rather than having to bloat out the code with loads of guff to deal with this insanity!! I just want to shadow a table of default values by setting and removing them in the top level of the code, it’s not much to ask.
I’ve found that I very seldom have to use metatables. Maybe try using classes instead. Metatables are mainly a detour created by all the lua tutorials online imo.
I’d only use metatables for something intense that I might wanna try scrape 5% cpu time off. Or if I, for some bad reason, want some strange structure where indices can be accessed as arguments.
I have used classes quite a lot in the tool i am writing, but they do not solve the problem exactly in this instance, and in fact i am totally against the OOP style for the purpose as it obfuscates what is actually happening in the code. That luabind magic is just a wrapper around the normal lua class style which is dependent on metatables. If you have any interest in seeing what i am trying i have pushed my experiments with metatables to the repo here: https://github.com/cupcake99/PushyPushPush/tree/metatable-hell
My plan now is to totally rewrite the tool taking out all the class nonsense, and all the junk i have had to write to deal with lua being lua, and make the thing just data and operations on that data, as that seems to me to be a more straightforward style for my purposes, and i have no need to be creating ‘objects’ dynamically (doubting that anybody will be mad enough to want to use more than one Push to control renoise (hoping Hitoritori doesn’t come looking…!)).
It took me a day or two just to get reacquainted with my project after losing interest for months… too convoluted, so much back and forth and trying to remember what object is being operated on…
To each his own
Classes to me are mainly about readable code and I’d recommend anyone to make everything classes. Just to avoid function-oriented spaghetti code that passes data back and forth. Different perspectives Indeed