lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Indeed I will probably be going with a weak table to track "back references" myself, for all the reasons noted. I was just wondering if there was some way maybe to leverage the GC system to help do that more automatically. I don't want to alter the VM though.

I'll explain a bit about why I was thinking about this, in case anyone has suggestions.

I've been wondering if I could somehow automate publish/subscribe (observer) to make reactive programming a little easier. I only want to do it for some sets of objects. In my case, for keeping various view objects in sync with model objects. Basically, a way to automagically hook up the view objects to the model obejcts, or individual view properties to model properties, possibly with transformations (processing) in between. Such that when the model changes, the view changes. Normally this would involve the view (or view construction) code subscribing, and the model code knowing enough to publish changes. I wanted to avoid (or somehow automate) more of the hook up code, subscribing, maybe even publishing, to some extent. Probably not perfectly, but some kind of improvement.

I have read a bit about LuaGravity (reactive programming in Lua) but it requires an altered VM, I don't want to do that.

So, in that context, any suggestions? Some way to automate the hook ups or the actual update of objects referring to objects when the referred objects change? (Or their properties?)




On Fri, Sep 13, 2013 at 7:48 PM, Tim Hill <drtimhill@gmail.com> wrote:

On Sep 12, 2013, at 7:00 AM, Marc Lepage <mlepage@antimeta.com> wrote:

> Suppose I have tables referring to tables (by their field values), or functions referring to upvalues (by closure).
>
> Given a table, I can know what it refers to by iterating over its contents. And given a closure, I can use the debug facilities to find its upvalues. I can do both recursively to obtain all objects or upvalues referenced.
>
> Is it possible to go in the other direction? To find all references to a given value (say a table) in other tables or closures (as upvalues)?
>
> Wouldn't the garbage collector have this information? It's not exposed in any debug facilities, is it?
>
> I'm just pondering about the idea of one object being able to notify other objects that reference it, without having to keep a list of listeners that explicitly subscribed for notifications.

You really don't want to use the GC for this (not that you can), and I would advise against the debug library for any production code (it makes very few claims regarding performance). There are lots of design patterns that could help you out with this problem, without having to resort to "tricks" .. your best friend here is a weak table, as what you are really looking for is a variant on pub/sub, and a weak table is the ideal way of tracking subscriptions.

--Tim