lua-users home
lua-l archive

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


One more question on the topic...in the below listing, the call gl.a()
works, but the call gl.b(1) produces an error, saying that "t" is
nil...looks like the parameter is not being passed.  How do I get around
that?

tab = {}

function tab:test1()
	print ("Hello from test1")
end

function tab:test2(t)
	print ("Hello from test2 " .. t)
end

gl =

	a = tab.test1,
	b = tab.test2
}

gl.a()
gl.b(1)

> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Eric Ries
> Sent: Friday, November 16, 2001 11:42 AM
> To: Multiple recipients of list
> Subject: RE: Function Pointers?
>
>
> Just like you are doing, except without the first set of ()
> around do_cmd1,
> etc.
>
> So, just to be clear:
>
> function foo()
> -- do something
> end
>
> myTable = { foofunc = foo,
>  		barfunc = function ()
> 				-- do something else
> 			    end
> 	    }
>
> myTable now has two "function pointers" that you can use like this:
>
> local a = myTable.foofunc
> local result = a()
> local b = myTable.barfunc
> local result2 = b()
>
> Eric
>
> > -----Original Message-----
> > From: owner-lua-l@tecgraf.puc-rio.br
> > [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of John S. Miley
> > Sent: Thursday, November 15, 2001 9:25 PM
> > To: Multiple recipients of list
> > Subject: Function Pointers?
> >
> >
> >
> > How would I go about emulating function pointers in lua?
> Basically, what
> > I'm trying to accomplish is something like:
> >
> > gCmdList = {
> > 	cmd1 = do_cmd1(),
> > 	cmd2 = do_cmd2(),
> > 	...
> > }
> >
> > gObj1 = {
> > 	[properties]
> > 	lCmdList = {
> > 		cmd3 = do_cmd3(),
> > 		cmd4 = do_cmd4(),
> > 		...
> > 	}
> > }
> >
> > gObj2 = {
> > 	[properties]
> > 	lCmdList = {
> > 		cmd5 = do_cmd5(),
> > 		cmd6 = do_cmd6(),
> > 		...
> > 	}
> > }
> >
> > function do_cmd1()
> > 	block
> > end
> >
> > function do_cmd2()
> > 	block
> > end
> >
> > function gObj1:do_cmd3()
> > 	block
> > end
> >
> > function gObj1:do_cmd4()
> > 	block
> > end
> >
> > function gObj2:do_cmd5()
> > 	block
> > end
> >
> > function gObj2:do_cmd6()
> > 	block
> > end
> >
> >
> > Thanks,
> >
> > --jsm
> >
> >
> >
>