lua-users home
lua-l archive

Re: Another example for syntactically lightweight closures

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


David Given <dg@cowlark.com> writes:
> OTOH it would be nice to have some way of avoiding the ugly end) after a
> function closure in a parameter list.
It seems like there are two rather different cases for an alternate
lambda syntax.
 (1) The "super lightweight so I can stick it in a function call with
 an almost trivial expression and it won't look clumsy". This is
 where you're not trying to hide the fact that it's a lambda (and
 don't mind the function call parens), but the current syntax is
 just too heavyweight.
 In this case, I think you want to get rid of all keywords, so it's
 less visually heavyweight, and looks "more expression-like".
 An example might using |arg1, ...| for arguments, and allowing
 either a parenthesized expression, or braces with a normal body:n
 |a, b|(a + b)
 => function (a, b) return (a + b) end
 |a|{ print (a) }
 => function (a) print (a) end
 Usage examples:
 table.foreach (tab, |x|{ print (x + 1) })
 => table.foreach (tab, function (x) print (x) end)
 quit_button:connect ('clicked', ||{ gtk.main_quit () })
 => quit_button:connect ('clicked', function () gtk.main_quit () end)
 [Note, in the last example, "gtk.main_quit" isn't passed in
 directly, because it complains if it gets any arguments, and the
 caller of that lambda passes in some arguments.]
 
 (2) As a simple way to add new control structures, by using
 smalltalk/ruby-like "blocks" to make function calls look sort of
 like control-structure.
 This is what I guess the "do ... end" proposal is supposed to
 address, but it seems kind of hard to do in Lua, as Lua
 control-structures are inherently "syntaxy". All of the attempts
 I've seen tend to look pretty bad (including the "do...end"
 proposal).
 I don't know if it's even possible to do this really well without
 using a meta- layer like metalua or some macro package -- and my
 personal feeling is that if it can't be done really well, I'd
 rather just stick with the current slightly-clumsy-but-clean
 syntax that add a kludgy ugly solution.
So my vote is "yea" on a lightweight syntax like (1) -- I use that kind
of thing all the time, and I'd love a more svelte way to express it --
but "nay" on a "control-structure" lambda like (2), unless somebody has
a much better idea than those which have been proposed so far...
[there's always metalua and the like for people that really want (2),
after all]
-Miles
-- 
Genealogy, n. An account of one's descent from an ancestor who did not
particularly care to trace his own.

AltStyle によって変換されたページ (->オリジナル) /