lua-users home
lua-l archive

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


Frank Black wrote:

> local listFiles
> listFiles = function(name)

Unless you want to declare the function in one place and
define it in another, a more common way to make a local
function is:

  local listFiles = function(name)

or (arguably even better):

  local function listFiles(name)

I don't know anything about LuaInterface, so consider the
following to be pseudocode, but you'll track down the error
more easily with better error checking, something like this:

  local function listFiles(name)
    Console.Error:WriteLine(name)
    local dirinfo, errstr = luanet.System.IO.DirectoryInfo(name)
    if dirinfo then
      local subdirs, errstr = dirinfo:GetDirectories()
      if subdirs then
        for i, subdir in ipairs(subdirs) do
          listFiles(subdir.FullName)
        end -- for i, subdir
      else
        Console.Error:WriteLine(errstr)
      end -- if
    else
      Console.Error:WriteLine(errstr)
    end -- if
  end

The general principle is to check return values rather than
assuming that no error occurred.  If a function might return
either a table or nil, then code that assumes it returned a
table should never get reached if it returned nil.
(LuaInterface may have a different way of signalling errors
than I'm assuming, but the principle still holds.)

(Also note the use of ipairs instead of a numeric for; when
possible, have the language to worry about loop indexing for
you.)

-- 
Aaron