[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lightweight function syntax (again) Re: [ANN] Lua 5.2.0 (alpha-rc2) now available
- From: Michal Kottman <k0mpjut0r@...>
- Date: Tue, 23 Nov 2010 16:32:48 +0100
On Tue, 2010-11-23 at 15:50 +0100, Axel Kittenberger wrote:
> Optically I like pipes too, since they also remind me of their use in
> the shell to "tie things together". Their main problem tough is they
> cannot nest, as the begin and end token is identical. One could say,
> if you want nesting use full function syntax please, but well maybe
> someone has a problem with it not working.
>
> | a, b => | x => x* x * x | (a+b) |
>
> -> supposedly a real challenge for the parser.
Metalua solves this really nicely, it also uses pipes [1], but in a
different way - it uses it to enclose the parameter list:
"|a,b| a + b" means "function (a,b) return a + b end"
The return is implicit, so "|args| exp" is actually "function(args)
return exp end". It does not allow to use statements (you have to write
the full "function() ... end" for that), but does nest nicely:
|x||y| x + y
translates to:
function(x)
return function(y)
return x + y
end
end
One disadvantage with this syntax is that it is not clear what happens
with multiple return values, i.e.:
print(|| true, false)
Is this 1 argument to print() - an anonymous function with 2 return
values, or 2 arguments - one function with 1 return value and the other
'false'?
Currently Metalua uses the second option - it allows only 1 expression,
because the other option caused problems [2].
[1] http://metalua.luaforge.net/manual003.html#toc3
[2]
http://github.com/fab13n/metalua/blob/master/src/compiler/mlp_ext.lua
- References:
- [ANN] Lua 5.2.0 (alpha-rc2) now available, Luiz Henrique de Figueiredo
- Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, Petite Abeille
- Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, Luiz Henrique de Figueiredo
- Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, Petite Abeille
- Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, Pierre-Yves Gérardy
- Lightweight function syntax (again) Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, Mark Hamburg
- Re: Lightweight function syntax (again) Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, steve donovan
- Re: Lightweight function syntax (again) Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, Mark Hamburg
- Re: Lightweight function syntax (again) Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, Pierre-Yves Gérardy
- Re: Lightweight function syntax (again) Re: [ANN] Lua 5.2.0 (alpha-rc2) now available, Axel Kittenberger