lua-users home
lua-l archive

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


On Wed, Jul 6, 2011 at 3:09 PM, David Hollander <dhllndr@gmail.com> wrote:
> why not disrequire the use of the 'function' keyword when 'local' is present?
> local success(items, total)

Not to argue for that (for reasons others have identified), but
sometimes I find myself accidentally writing that.  Along similar
lines, it has been pointed out before that it would be very natural if
we could write Lua functions more mathematically like this:

  f(x,y) = x*y

That follows table assignment syntax, which is also nice since tables
and functions are often interchangeable concepts of mappings [1,2].
Unfortunately, there were some lexical problems with this syntax in
the context of Lua--IIRC, perhaps again related to Lua's overloading
the use of parenthesis.  It is possible even to hack this syntax in
Lua 5.1 via an __index metamethod on the global environment
auto-generating "placeholder expressions" [3,4], but that has too much
weirdness to be used seriously:

  f[{x,y}] = x*y  -- valid in Lua 5.1 under suitable crazy definition
of environment

IMO, the Lua syntax of both `local` and `function` can get a little
clunky/distracting as has been pointed out by various people before,
but I'm not sure what the grand solution is:

  local x = 1 -- `local` is five characters. Compared: "my $x = 1"
(Perl) and "a := 1" (Google Go)
  local foo
  local function bar() foo() end
  function foo() bar() end  -- forward declaration of local function
misleadingly looks like global
  M.foo = foo  -- redundant syntax to declare function both locally
and in module table
  local z = (function() local x = f() return x*x)()  -- kind-of slow
and verbose to do in one statement
  local baz; do local x = f(); baz = x*x end -- good alternative but
repeats `baz`

[1] http://lua-users.org/wiki/FuncTables
[2] http://lua-users.org/wiki/MutableFunctions
[3] http://lua-users.org/wiki/SymbolicDifferentiation
[4] http://lua-users.org/wiki/ExpressionTemplatesInLua