[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: string.gsub accepting a callable userdata
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Wed, 12 Dec 2012 13:50:48 +0000
2012/12/12 steve donovan <steve.j.donovan@gmail.com>:
> string.gsub is one of the most powerful Lua functions we have, and the
> substitution may be a string, a table, or a function.
>
> It would be useful if this list could be extended to include callable
> userdata - in most language interop situations I've encountered,
> foreign functions are represented in this way on the Lua side.
>
> (It would be too confusing to allow callable _tables_ of course)
Userdata can also be indexable, and it may be useful for gsub to allow
indexable userdata too (with the same semantics as when passed a
table). But since Lua has no way to easily detect whether a value is
indexable or callable, IMHO it's a sane fallback to require a table
when you want the indexing behaviour, and a function when you want the
calling behaviour. You can always easily wrap an indexable object or a
callable object in a proxy table or a function respectively.
So is there any reason for the change you request besides a little
performance improvement (not creating a closure) in an arguably
specific use case? Also would you have any argument to favour the
__call operation over the __index operation if the passed value type
is "userdata"?