[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Lightweight function syntax (again) Re: [ANN] Lua 5.2.0 (alpha-rc2) now available
- From: Mark Hamburg <mark@...>
- Date: Sun, 21 Nov 2010 10:52:28 -0800
On Nov 20, 2010, at 5:18 AM, Pierre-Yves Gérardy wrote:
> My favourite syntax so far is
> @[ a,b => a+b ]
> @[; dostuff() ]
That's probably one of the best proposed syntaxes I've seen to date.
A C# inspired possibility but I haven't checked the grammar is:
( arg list ) => expression
( arg list ) => do statements end
Though in that case, we could presumably also use the | token and avoid syntactic ambiguity:
| arg list | expression
| arg list | do statements end
I agree that 'function' gets a bit too heavy weight visually when trying to do more functional-ish programming. (I've been digging into reactive frameworks lately.) But in any of these cases, we need to figure out syntactic precedence. I would guess that it would sit at about the same level as table construction.
The other feature needed is treating these like tables and strings and allowing them to be used for parenthesis free function calling:
myStream:map @[ x => x * x ]
:filter @[ x => x % 2 == 0 ]
:subscribe @[ x => do
total = total + x
log:writef( "%d %d", x, total )
That example, however, leads to the question: Where was 'total' declared and initialized?
Finally, in any of these use cases, it's worth remembering the distinction between blocks and functions when it comes to return and break. This is something that Smalltalk and I think Ruby dealt with but other languages only stumble onto later. (Smalltalk has two return operators. One returns a value from the block. The other returns from the context in which the block was created.) Any Lua solution should probably try to guide people toward thinking of these as lightweight functions rather than as blocks of code.