lua-users home
lua-l archive

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



On Tue, Sep 20, 2011 at 1:22 PM, Thijs Schreijer <thijs@thijsschreijer.nl> wrote:
Probably a stupid thingy, but can't seem to get it;

This code (part of a larger object)
20      -- add a filter to my list, no duplicates will be added, flt is
string or table
21      add = function (self, flt)
22              if type(flt) == "string" then
23                      flt = self:split(flt)
24              end
25              assert(type(flt) == "table", "cannot add filter, string or
table expected, got " .. type (flt))
26              if not flt.filter then
27                      flt.filter = string.concat(flt, ".")
28              end
29              if not self.filters[flt.filter] then -- only add if not in
the list already
30                      self.filters[flt.filter] = flt
31              end
32      end,

When called with this test;
133     local s = pcall(filters:add(123))
134     assert( not s, "error expected because of a number")

Gives me this error;
lua: xplfilter.lua:25: cannot add filter, string or table expected, got
number
stack traceback:
       [C]: in function 'assert'
       xplfilter.lua:25: in function 'add'
       xplfilter.lua:133: in main chunk
       [C]: ?

Question is; The error is obvious, but why doesn't the pcall on line 133
catch the error and return true in local s?

Lua for windows, code uses only 1 external require; "loop.simple" for the
object creation.


You are invoking the function before allowing pcall() to do its thing.

You probably want this:

   local s = pcall(add, filters, 123)

This invokes the function add on the remaining arguments in a protected manner.

Robby