lua-users home
lua-l archive

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


On Mar 1, 2012, at 3:34 AM, Roberto Ierusalimschy wrote:

>> What if the lightweight syntax didn't support functions with
>> upvalues? One could still reference globals, but a reference to
>> a lexically bound but non-local variable would be an error in a
>> lambda expression, whatever the syntax. This would mean that lambdas
>> could be allocated once at load time for the bytecode rather than
>> at each instantiation. The distinction from functions would be
>> justified/clarified by the fact that these are created using different
>> syntax.
>> 
>> Are these still interesting?
> 
> I think we arrive at a very restricitve form of functions: no upvalues
> and one simple expression. In particular, for functions with no
> upvalues, the idea of giving them a name seems even more appealing,
> because you may even have a library of useful small functions.
> 
> PS: Lua 5.2 already does that optimization of creating only one closure
> instance for functions with no upvalues, despite the heavy syntax.

After writing my initial message, I concluded that this wasn't all that interesting. It helps with things like "make a field extractor for the field foo" which is probably easier to read and write as something like "\x(x.foo) than as "function_utils.extractor 'foo'" since one only has to know a little syntax for the former v knowing a library for the latter. But it doesn't handle things like "give me a function to invoke message baz on object zerp — i.e., the thing being asked for when asking that "zerp:baz" be valid syntax outside of using it to call a method.

Mark

P.S. I knew about the 5.2 optimization but of course it's harder to predict at a glance when it will apply, so if one obsesses over these details it doesn't help as much. (I don't obsess. I simply appreciate that 5.2 is smart when it can be.)