Tables of Functions

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Tables of Functions

Ken Smith-2
I would like to make an array of functions.  I tried the following syntax.

function steps[1] (a,b)
  print("steps[1]",a,b)
end

This yields the error message:

lua: test.lua:1: '(' expected near '['

If I write this instead, it works.

steps[1] = function (a,b)
  print("steps[1]",a,b)
end

Am I justified in expecting the first flavor to work?

 Thank you,
 Ken Smith

Reply | Threaded
Open this post in threaded view
|

Re: Tables of Functions

Luiz Henrique de Figueiredo
> Am I justified in expecting the first flavor to work?

No. The reference manual says

        funcname ::= Name {'.' Name} [':' Name]

See http://www.lua.org/manual/5.1/manual.html#2.5.9
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Re: Tables of Functions

Ken Smith-2
On 9/25/06, Luiz Henrique de Figueiredo <[hidden email]> wrote:
> Am I justified in expecting the first flavor to work?

No. The reference manual says

        funcname ::= Name {'.' Name} [':' Name]

In that case, I might expect the following to work but it doesn't.

function steps.1 (a,b)
 print("steps[1]",a,b)
end

Can you tell me why?

 Thank you,
 Ken Smith

Reply | Threaded
Open this post in threaded view
|

Re: Tables of Functions

Eric Scouten

On 25 Sep 2006, at 16:49, Ken Smith wrote:

On 9/25/06, Luiz Henrique de Figueiredo <[hidden email]> wrote:
> Am I justified in expecting the first flavor to work?

No. The reference manual says

        funcname ::= Name {'.' Name} [':' Name]

In that case, I might expect the following to work but it doesn't.

function steps.1 (a,b)
 print("steps[1]",a,b)
end

Can you tell me why?

name as defined above has to be a valid identifer. 1 doesn't count. For instance,
	
	function steps.foo(a, b)

is a valid alias for

	steps[ "foo" ] = function(a, b)


-Eric



Reply | Threaded
Open this post in threaded view
|

Re: Re: Tables of Functions

Ken Smith-2
Right.  Ok.  I can use the non-syntactic sugar version to create my
list of functions.  Thank you for the responses.

 Ken Smith

Reply | Threaded
Open this post in threaded view
|

Re: Re: Tables of Functions

Glenn Maynard
On Mon, Sep 25, 2006 at 04:58:59PM -0700, Ken Smith wrote:
> Right.  Ok.  I can use the non-syntactic sugar version to create my
> list of functions.  Thank you for the responses.

Or just

list = {
  function() return "a" end,
  function() return "b" end,
  function() return "c" end,
}
print(list[2]())

-- 
Glenn Maynard

Reply | Threaded
Open this post in threaded view
|

Re: Re: Tables of Functions

Sam Roberts-2
In reply to this post by Ken Smith-2
On Mon, Sep 25, 2006 at 04:58:59PM -0700, Ken Smith wrote:
> Right.  Ok.  I can use the non-syntactic sugar version to create my
> list of functions.  Thank you for the responses.

You may know, but if defining functions in order ([1], [2], ...) you also can do:

vtable = {
    function (x)
        print(x)
    end,
    function (y)
        print(y*2)
    end,
    -- ...
}

t[2](5)