They don't really conflict at all. You're storing a reference to a table; i.e. a memory address. If you share that address between two closures, they can both change the contents stored at that address. If you store a number in that upvalue instead, altering it in one closure wouldn't affect the upvalue of the other. It's kind of like the difference between passing an int to a C function versus passing a pointer to a struct to a C function. -Rob On 2011年4月08日 23:04:36 +0200 Anthony Howe <achowe+lua@snert.com> wrote: > On 08/04/2011 21:16, Rob Hoelz whispered from the shadows...: > > If you want C closures to share values, you could store a table as > > an upvalue, store your values in there, and set the same table as an > > upvalue for the closures. > > Ugh. One person says no. Another suggests the very thing I want to try > that prompted the question. > > In 5.1 does... > > lua_newtable(L); > lua_pushvalue(L, -1); > lua_pushcclosure(L, &foobar, 1); > lua_pushcclosure(L, &barblat, 1); > > -- rinse & repeat > > Work? Can more than one C function share a single upvalue? I ask > because something along these lines has been mentioned in Lua 5.2 as > an alternative to environments or some such without any examples. So > I'm unclear as to whether this is possible in 5.1 and/or 5.2. > > So far I have conflicting answers. > > > > > -Rob > > > > On Fri, 8 Apr 2011 15:05:31 -0400 > > Patrick Donnelly <batrick@batbytes.com> wrote: > > > >> On Fri, Apr 8, 2011 at 1:46 PM, Anthony Howe <achowe+lua@snert.com> > >> wrote: > >>> Can Lua 5.1 upvalues be associated with more than one C closure? > >> > >> No. > >> > > >
Attachment:
signature.asc
Description: PGP signature