lua-users home
lua-l archive

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

> > On Mar 24, 2017, at 10:26 AM, Coda Highland <> wrote:
> > 
> > 
> > Well yes, of course, you make a weak-valued table with lightuserdata
> > keys and full userdata values and marshal your pointers through that,
> > but it's not immediately obvious and there are a number of possible
> > pitfalls in the implementation (for example, forgetting that
> > weak-valued tables are a thing makes this solution either ridiculously
> > leaky or annoyingly difficult to maintain).
> > 
> > This has come up a number of times before, so while I don't think that
> > "per-value metatables" is the right solution to the task, it does at
> > least indicate that the issue is fairly common.
> > 
> I don’t think is a particularly complex design pattern, but I do agree it might be one that is worth documenting somewhere (in the wiki perhaps?). In any case the OP was suggesting a core design change in Lua as a solution when I think this pattern does all he needs.

This "annoyingly difficult to maintain" implementation goes by the name
"memoization" (for the function that, given a void*, encapsulates it
into a full userdata). It is a textbook example of the use both of light
userdata and weak tables. (This kind of use was the main motivation for
the introduction of light userdata in Lua.)

-- Roberto