lua-users home
lua-l archive

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


On 2015-06-12, at 6:46 PM, Rena <hyperhacker@gmail.com> wrote:
> 
> On Fri, Jun 12, 2015 at 2:04 PM, Brigham Toskin <brighamtoskin@gmail.com> wrote:
>> Don't forget we would end up with very ruby-like method chaining, for
>> methods that don't require any additional args...
>> 
>>> 
>>> monster:selectpartymember:takedamagewithresistance(monster:selectspell:randomdamageval)
>> 
>> I'm not arguing the merit of this one way or the other; just pointing out
>> it's a consequence for calling methods like this.
>> 
> 
> That can be nice when you have more reasonable method names :-)
> 
> command = input:urldecode():lower():trim():split(' ')

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.

The strongest motivation for this is a way to write special-form-like things in little languages. Consider a Python-like with:

with(file_handle) in do
  file_handle:write("foo")
end

function with(o)
  o:open()
  goto in(o)
  o:close()
end

…except you want to pcall(goto in()). Oops. Well, exception handling in Lua was never very pretty anyway. Like C's vprintf, there needs to be some way to pass your block through to another call.

Jay