lua-users home
lua-l archive

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


It was thus said that the Great Nevin Flanagan once stated:
> 
> On Jan 13, 2010, at 9:18 PM, Joshua Jensen wrote:
> 
> > I'm assuming (from the recent message referring to 'continue') that Lua
> > 5.2 does not have a continue keyword?  I use continue almost every time
> > I write a script, and it would sure be nice to have it an integral part
> > of the language.  The continue patch works fantastic for me.
> 
> While I understand that the phrasing can be noticeably prettier and easier
> to maintain in some circumstances, is there something "if condition then
> continue end" does that "if not condition then rest_of_loop end" can't?

  From some C code I've written:

void handle_sigchld(void)
{
  pid_t child;
  int   ret;
  int   status;

  while(1)
  {
    child = waitpid(-1,&status,WNOHANG);

    if (child == (pid_t)-1)
    {
      if (errno == ECHILD) return;
      if (errno == EINTR) continue;
      (*cv_report)(LOG_ERR,"$","waitpid() returned %a",strerror(errno));
      return;
    }

    /* rest of function snipped */
  }
}

  Due to the nature of C and the reporting of errors from system calls, I'm
not sure how I would restructure this to remove the use of "continue" (just
for the unititiated---waitpid() returns -1 on error, with the actual error
in the global variable errno [1]; when errno is EINTR that means that the
system call was interrupted and you should try your system call again).  The
intent of this code is to wait for all pending children processes to end
(there may be more than one), thus the loop.  

  Hmmm ... using Lua (and assuming the API exists)

	while true do
	  child,status,error = waitpid(-1,WNOHANG)
	  if error == 0 then
	    -- do success case
	  elseif error == ECHILD then 
	    return
	  elseif error ~= EINTR then
	    log(error)
	    return
	  end
	end

  I guess with multiple value return, I might not need continue (although
there have been a few times I've missed it, but I've forgotten under what
circumstances it's been).  

  I do, however, like the ability of continue to avoid an additional level
of nesting that I might otherwise need.

	while true do
	  child,status,error = waitpid(-1,WNOHANG)

	  if error ~= 0 then
	    if error == EINTR  then continue end
	    if error == ECHILD then return end
	    log(error)
	    return
	  end

	  -- do success case
	end

  To me this looks cleaner.

  -spc (I think I got the first Lua fragment correct ... )

[1]	To be pedantic, errno returns a "modifiable lvalue"