View Single Post
07/01/14, 05:22 AM   #13
zgrssd
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 280
getSet Pattern Mk.2 - now with Metatables.
Finally got around to properly reading into thier basics. With Metatables I am able to set up functions to be called in case a index is not set(metamethod __index).
I can also set up a function to do work if somebody tries to set a index that does not exist yet (metamethod __newindex)
With those two I can get a lot closer to simulating Properties in Lua, while even retaining all the syntax sugar of them (that they can be accessed like other fields). I just redirect every get and set attempts to my functions, preventing the table value from ever being set.

Code:
--accessing a index that was never defined reading
Instance["SomeValue"]
--Is interpreted like this with a metamethod around:
(getMetatable(Instance).)__index(Instance, "SomeValue")
Code:
--trying to write a index that currently has no value
Instance["SomeValue"] = "Hello World"
--Is interpreted like this with a metamethod around:
(getMetatable(Instance).)__newindex(Instance, "SomeValue", "Hello World")
I still need to figure out how to hide the backing fields from external manipulation, but I got some ideas already (like preventing the metatable from being set or seen and just puttig it there)

Of course the whole thing can be circumvented by getting the raw value (while ignoring metamtehods). But if somebody tries that, he or she better knows what the heck they are doing there.
  Reply With Quote