Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created January 15, 2012 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isaacs/1613485 to your computer and use it in GitHub Desktop.
Save isaacs/1613485 to your computer and use it in GitHub Desktop.
// block-lambda proponents: what does this program do?
function omgnomakeitstopkillitwithfire (t) {
x = {|y|
return "returning: " + y
}
// now here comes a bunch of code.
// imagine there are 50 lines here
t(x)
// now a bunch more stuff.
// eventually:
x("there")
// then a bunch more stuff
// ending with:
return 100
}
var ret = omgnomakeitstopkillitwithfire(function (cb) {
alert("before")
cb("here")
alert("after")
})
alert(ret)
/**
* HOW DOES THIS NOT QUALIFY AS INSANE ACTION-AT-A-DISTANCE???
* Seriously, this belongs in the bad-parts bucket with `with` and `eval`.
**/
@grncdr
Copy link

grncdr commented Jan 17, 2012

After looking at this for while, I can't imagine many scenarios where I'd want to have both a block lambda return and a "normal" return in the same function. It seems to me that disallowing mixing of return types in the same (static) scope, would remove at least one obvious way to write confusing code without giving up a lot of expressivity.

In the scenarios where you do want to return "normally" you could use an immediately invoked block like ({|| return <expression>})(). Obviously that's a lot of syntactic overhead compared to return <expression>, but due to being an uncommon use, the explicitness might not be unwelcome.

@BrendanEich
Copy link

@grncdr: stretch your imagination with some Smalltalk-80 code. There are plenty of uses for mixed local and non-local returns. See https://github.com/allenwb/ESnext-experiments/blob/master/ST80collections-exp0-blp.js for a start.

/be

@grncdr
Copy link

grncdr commented Jan 18, 2012

@BrendanEich, much better example of used mixed returns, I retract my previous suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment