block lambda revival

Brendan Eich brendan at mozilla.com
Sat Jun 4 07:47:45 PDT 2011


On Jun 4, 2011, at 6:32 AM, Peter Michaux wrote:

> On Fri, May 20, 2011 at 5:54 PM, Brendan Eich <brendan at mozilla.com> wrote:
>> As promised/threatened: http://wiki.ecmascript.org/doku.php?id=strawman:block_lambda_revival
> 
> The examples on the wiki page feature calling a function that takes
> one block argument and also feature Tennent's correspondence principle
> nicely; however, I don't see examples of how to define functions
> taking one or more block arguments or how to call a function with two
> or more block arguments. Could someone add an example? Perhaps a
> Boolean.prototype.ifTrueIfFalse definition equivalent to Smalltalk's
> ifTrue:ifFalse: would fill these example gaps?

Sure; the grammar support is there in the strawman, see CallWithBlockArguments. There's nothing special about this restricted kind of call expression on the callee's definition side. Any function can be invoked this way.

Object.defineProperty(
  Boolean.prototype,
  'ifElse',
  {
    value: function (ifTrue, ifFalse) {
      return this ? ifTrue() : ifFalse();
    }
  }
);

let aBool = Math.random() >= 0.5;

print(aBool.ifElse {|| "true"} {|| "false"});

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110604/3f69d291/attachment.html>


More information about the es-discuss mailing list