lua-users home
lua-l archive

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

On Thu, Apr 27, 2017 at 5:43 PM, Soni L. <> wrote:
So, out of all syntax proposals for lambas, I'm surpised nobody thought of this beauty.

You see, we have `local x = function(a,b,c) return thing end` syntax. BUT:

1. It's a syntax error to use `function(a,b,c) =thing end`, even if the REPL accepts `=` as an alias for `return`.
2. Do we need `end` if we restrict it to a single _expression_?

Combine these 2, and the simplest solution seems to be:

local x = function(a,b,c) = thing

AND we can still get multiple returns:

local x = function(a,b,c) = select(1,a,b,c)
print(x(1, 2, 3)) --> 1, 2, 3

The idea is, function(a,b,c) starts a plain old function. If the next token is a =, this has to be a lambda. Otherwise it has to be a function.

There's no ambiguity here since the proposed syntax is currently a syntax error. It also looks very much like Lua.

The following looks nice:
local swap = function(a,b) = b,a

But what does the following nested lambda
local x = function(a,b) = function() = a, b
local x = function(a,b) = (function() = a, b)
local x = function(a,b) = (function() = a), b