Created
January 15, 2012 00:02
-
-
Save isaacs/1613485 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: 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
@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
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 toreturn <expression>
, but due to being an uncommon use, the explicitness might not be unwelcome.