lua-users home
lua-l archive

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


On Mon, Apr 9, 2012 at 03:52, 桑伟 <gsunwaym@gmail.com> wrote:
> Hi, all
> Sorry for this stupid question. I try to keep it simple. I have two files:
> -- file1 --------------
> module(..., package.seeall)
> function new()
>   local t = {}
>   function t:p(params)
>     print(params.name)
>   end
>   return t
> end
> --- file2 -------------
> f1 = require("f1").new()
> local t = { name = "haha" }
> f1.p(t)
>
> When I run "lua52 f2.lua" at my macbook. I got these error messages:
> lua52: ./f1.lua:6: attempt to index local 'params' (a nil value)
> stack traceback:
> ./f1.lua:6: in function 'p'
> f2.lua:3: in main chunk
> [C]: in ?
>
> Anybody can help?

You declare p as: function t:p(params)
but then you call it as: f1.p(t)
Note the colon in the declaration. That makes it equivalent to:
function t.p(self, params)
So when you call it with a period, you're passing t as the hidden
'self' parameter, not the 'params' parameter.
You need to write either f1:p(t) or f1.p(f1, t).

-- 
Sent from my toaster.