Proposal for Ruby-style anonymous block functions (that don't kill the indention)

This list had a proposal last month to make everything an expression, and it has not infrequent attempts to create multi-line lambdas, and I think the reason for this is that people want a better way to create functions that act like Ruby-style blocks since sometimes it makes more sense to write the function that will be passed after the thing it will be passed to and not before. So, here's my proposal. I expect it to get rejected, but hey, someone approved @decorator, so maybe it will make it... Instead of
where the def inner comes before you know what it's going to be used for, why not
? Here's it's very straightforward that what you're doing is returning an anonymous function, and then you find out what's in the function. Similarly,
Which will be equivalent to:
Again, I think it's clear here to be told first, "Oh, we're going to sort something" and then learn how the sorting will be done than it is to read a weird function and only after that learn what it's for. Caveats: This shouldn't be allowed to work with two @s in one line. If you have two of them, you should give them names ahead of time with def. This also should not be allowed work inside a for-, if-, with-, or while- statement's initial expression, since the indenting won't work out. Also, myfunc = @(args) should be preemptively banned, since def is the one right way to do it. Also you shouldn't be allowed to do this to make one liners with this (as you can eg. with if-statements), since that's why there's lambda. And using this on the declaration line of a decorator is just crazy. Of course, for all I know, Python's grammar is too simple to make this work with all the caveats, but if it's not, I think this might be a good way to improve readability without killing the indention-based nature of Python that we all know and love. My other thought is that if @ is deemed to be too "Perl-ish" of line noise, def could be used as the keyword instead. Or perhaps a new keyword like "block" or something. That said, I think there is a good analogy here to the existing @-decorator where things are, strictly speaking, written out of order so that the readability is improved and the function that follows is given as an argument to the initial @ line. Thoughts? -- Carl

On Sun, Nov 9, 2008 at 9:56 PM, Carl Johnson <carl@carlsensei.com> wrote:
This bothers me. You seem to have some kind of syntax error here, because you have two parens before the colon. I don't know if you meant to do this, but it seems very, very strange the way you have it, and the obvious alternative seems strange as well:
Since I can't think of a reasonable way to fix this, I'm going to have to -1 it. Also, I think in the light that lambda functions are just hard to work with in general (as seductive as they are when you're typing), I'd rather encourage people to formally specify any functions they are going to use, if only to give them a chance to be tested (Hey look, I'm promoting TDD! How out of character!). -- Cheers, Leif

No, the "extra" parenthesis is intentional. The original expression must terminate before the block begins. Not having the parenthesis there would mean that it needs to be added to end of the block, but then it wouldn't be the indentation that controls the block but the parenthesis, and that's unpythonic. The original idea I had was to have a special keyword for "variable which I will define on the next line, just wait a minute please" but then I simplified it down to @ on analogy to decorators. Still, some people might like the original idea more:
And then we just have to fight about what would be a good keyword or symbol for "deferred."

On Sun, Nov 9, 2008 at 9:56 PM, Carl Johnson <carl@carlsensei.com> wrote:
This bothers me. You seem to have some kind of syntax error here, because you have two parens before the colon. I don't know if you meant to do this, but it seems very, very strange the way you have it, and the obvious alternative seems strange as well:
Since I can't think of a reasonable way to fix this, I'm going to have to -1 it. Also, I think in the light that lambda functions are just hard to work with in general (as seductive as they are when you're typing), I'd rather encourage people to formally specify any functions they are going to use, if only to give them a chance to be tested (Hey look, I'm promoting TDD! How out of character!). -- Cheers, Leif

No, the "extra" parenthesis is intentional. The original expression must terminate before the block begins. Not having the parenthesis there would mean that it needs to be added to end of the block, but then it wouldn't be the indentation that controls the block but the parenthesis, and that's unpythonic. The original idea I had was to have a special keyword for "variable which I will define on the next line, just wait a minute please" but then I simplified it down to @ on analogy to decorators. Still, some people might like the original idea more:
And then we just have to fight about what would be a good keyword or symbol for "deferred."
participants (2)
-
Carl Johnson
-
Leif Walsh