Concise functions, Nonexistent lambdas, Explicit tail calls

Michael Day mikeday at yeslogic.com
Mon Dec 8 23:19:08 PST 2008


Hi,

This proposal consists of three ideas for concise functions, nonexistent 
lambdas, and explicit tail calls that go well together. It supersedes 
any previous proposals I may have made on these subjects :)

CONCISE FUNCTIONS

(1) Treat "fun" as a keyword that is exactly equivalent to the existing 
"function" keyword used in function expressions and declarations.

(2) A function body may consist of an expression wrapped in parentheses, 
which is evaluated as the return value of the function.

EXAMPLES OF CONCISE FUNCTIONS

fun()(false)
fun(elem) (elem.type == "text")
fun(x, y) (x + y)
fun(g, h) { g(); return h(); }

ADVANTAGES OF CONCISE FUNCTIONS

Both changes are very easy to implement and should be compatible with 
top-down parsers. The two changes combine to allow very concise function 
declarations, while still preserving the syntactic feel of JavaScript.

As a bonus, any future additions to expressions, such as comprehension 
expressions or other control structures, will be immediately usable in 
expression function bodies.

NONEXISTENT LAMBDAS

(1) Do not add "lambda" as a keyword and do not add any new lambda 
construct to the language.

EXAMPLES OF NONEXISTENT LAMBDAS

None, as they do not exist.

ADVANTAGES OF NONEXISTENT LAMBDAS

Nonexistent lambdas are already implemented in all the major JavaScript 
implementations, and have been in JavaScript since forever and do not 
require any further changes to the ECMA specifications.

Refraining from adding a new lambda construct will make JavaScript 
easier to specify, easier to implement, easier to teach and learn.

JavaScript already has functions as first-class values, and concise 
functions greatly reduce the syntactic overhead of using them, to the 
point where there is nothing to gain by introducing a new construct.

EXPLICIT TAIL CALLS

(1) Implementations should treat any ReturnStatement whose expression is 
a CallExpression as a tail call, except when the ReturnStatement is 
within a WithStatement or within a TryStatement.

Note: This does not cover tail calls in expressions, but implementations 
could at their option treat certain calls as tail calls within the body 
of function expressions. For example:

fun() (f(), g()) // <-- g could be a tail call

fun(f, g) (f() || g()) // <-- g could be a tail call

fun(x, f, g) (x ? f() : g()) // <-- f and g could be tail calls


EXAMPLES OF EXPLICIT TAIL CALLS:

fun f()
{
     return g(); // <-- tail call!
}

fun f()
{
     g(); // <-- not a tail call, no "return" keyword
}

fun f(x)
{
     with (x)
     {
         return g(); // <-- not a tail call, inside 'with'
     }
}

ADVANTAGES OF EXPLICIT TAIL CALLS

Very easy to specify and very easy to implement.

It makes tail calls explicit, and explicit tail calls help preserve 
correctness of code that depends upon them.

Best regards,

Michael

-- 
Print XML with Prince!
http://www.princexml.com


More information about the Es-discuss mailing list