Re: Function statement name syntax in Lua

mjmouse9999 <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
On Thursday, January 29, 2026 at 11:16:26PM UTC+10 Jure Bagić wrote:
> Is there any reason this syntax for the function statement name is not 
valid in
> Lua:
> > global mymodule = {};
> > local name = "OK";
> > function mymodule[name] (a, b, ...t)
> > ...
> > end
>
> In the above, the statement name of the function is invalid as mymodule 
table
> can only be indexed with '.' ('funcname' in the parser) and end with ':'. 
The
> only thing I can think of is that the '.' syntax looks better (more 
readable)
> and the potential errors in '[]' might be hard to debug (expressions => 
calls =>
> metamethods, etc...). However, still not convinced why such decision 
("crying
> baby noise").
> [...]

For the final function one reason could be because you have no equivalent 
way to
call it with a self parameter.

So we have the following for functions as sugar:

function module.this.that(a, b, c) end
-- equivalent to
module.this.that = function(a, b, c) end

function module.this:that(a, b, c) end
-- equivalent to
module.this.that = function(self, a, b, c) end

Is your question also that '.this' above can't be replaced with '[this]'?
(and the final function name is still .that or :that)

    ~$ lua -e 'function module[this].that(a, b, c) end'
    lua: (command line):1: '(' expected near '['

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/75043f72-03f3-4574-bd8e-946c7a42b6b3n%40googlegroups.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.