lua-users home
lua-l archive

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


On 2012-12-13 1:27 PM, "Jerome Vuarand" <jerome.vuarand@gmail.com> wrote:
>
> 2012/12/13 Rena <hyperhacker@gmail.com>:
> > You want __mode='v', not __weak.
>
> Yes, of course :-)
>
> > And I'm not sure why you're using a table
> > to store that one item. It's not necessary to use a table to create local
> > upvalues:
>
> But then you get a hard reference to the userdata object, and that may
> change its lifetime.
>
> > local gsub,type = string.gsub,type
> > local ref
> > local function unref(...) return ref(...) end
> >
> > function string.gsub(s,pat,repl)
> >    if type(repl) == 'userdata' then
> >       ref = repl
> >
> >       repl = unref
> >    end
> >    return gsub(s,pat,repl)
> > end
> >
> > Although this function just looks like an elaborate noop... is it needed at
> > all?
>
> What function, unref or the new string.gsub? The point of this whole
> topic is to be able to pass a callable userdata to string.gsub, which
> these two function achieve, so I don't think it's a no-op.
>

Aha, of course.
I realize what the function is doing, it just seems silly that wrapping something like that would have any effect. Which I guess is the point of the original post...

I always thought Lua would benefit if it provided functions cancall() and canindex(), and libraries (especially the standard libraries) used them instead of checking for specific types. It always seemed arbitrary and hackish when an object can be used transparently in most contexts, but needs special treatment when being passed to C functions (or a Lua function that checks for certain types). The same goes for __tostring.