lua-users home
lua-l archive

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

Rici Lake wrote:

In order for functions which might be used as iterators to be usable as iterators, it is important that they return <false, error>, rather than <nil, error>. However, having thought about this a bit, and tried both interfaces, I actually remain convinced that being able to access errors "after the fact" leads to simpler code. It is certainly possible to write:

  for in, errmsg in f:lines() do
    if errmsg then
      -- handle the error
    -- handle the non-error case

but it requires every script to think about the possibility of errors in order to avoid a runtime type error (boolean used where a string is required.) Maybe that's not a bad thing, but in many cases it is actually ok to just let a loop die if an error is encountered. Furthermore, errmsg in the above is local to the for loop, which is also a trifle awkward.

Since Lua is single threaded and coroutines do not implicitly yield, we could just have an errno property in the osex package variable. But then you have to think about dealing with errors if you ever need to yield and it does not allow for hooks that implicitly yield, like has been discussed here.

Ignoring the existing io package issues for a second, returning an error for most of the osex functions proposed is easy. But what object could be used to hold the error for the files() iterator? Any syggestions?

chris marrin              ,""$, "As a general rule,don't solve puzzles        b`    $  that open portals to Hell" ,,.
        ,.`           ,b`    ,`                            , 1$'
     ,|`             mP    ,`                              :$$'     ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`