lua-users home
lua-l archive

Re: blocks in Lua (was Re: [PROPOSAL] [PATCH] simple method call)

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


On Saturday, June 13, 2015 07:53:59 AM Jay Carlson wrote:
> This is the spot where I suggest Lua get blocks:
> 
> commands:foreach() in cmd do
> print(cmd)
> end
> 
> Where "foreach" looks like:
> 
> function foreach(l)
> for _,v in ipairs(l) do
> goto in(v)
> end
> end
> 
> Where "goto in()" is like a function call to the block parameter.
> 
> The block has no name. "goto in()" is special syntax like "…". There is no
> way for the callee to capture the block, and it is safe to allocate on the
> stack.
There's a whole lot of magic in what you describe, like the mysteriously 
scoped "in" function that isn't a function.
What you're doing is essentially
 function foreach(l,f) for _,v in ipairs(l) do f(v) end end
 commands:foreach(print)
 -- or rather
 commands:foreach(function(v)
 dosomething(v)
 end)
So following the shorthand "" and {} call with string/table syntax, how about 
a shorthand call with function syntax
 args ::= '(' [explist] ')'
 | tableconstructor
 | LiteralString 
 | functiondef
 functioncall ::= prefixexp args
and thus
 commands:foreach function(v)
 dosomething(v)
 end
Then again, PUC-Rio is just as likely to remove shorthand functioncall syntax 
as add any new ones. It's just a minor matter of convenience that doesn't 
change the expressibility of the language significantly.
-- 
tom <telliamed@whoopdedo.org>

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