[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Idea: 'function' keyword optional when preceded by 'local'
- From: Xavier Wang <weasley.wx@...>
- Date: Thu, 7 Jul 2011 09:02:22 +0800
2011/7/7 David Manura <dm.lua@math2.org>:
> 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
>
>
Similar with this idea :)
maybe we can learn ruby way?
local a = do|x, y| x + y end
but maybe it's not a lua-style...
I think this kind of thing is great (apart of syntax, just idea):
local a = |x, y| x + y
or
local a = @(x, y) x + y
note that @ can not be appear the beginning of source, so it's okay
with chunkname.
or a C++ way:
local a = [](x, y) x + y
or some simpler:
local a = [x, y] x + y
or python way:
local a = lambda x, y: x + y
or some mathematic:
local a = @x, y -> x + y
these can easily make syntax sugar:
local a|x, y| .... end
local a@(x, y) ... end
local [a](x, y) ... end
local a@x, y -> ... end
is any of these easy to implement?