Req #54243 [Com]: Shorter syntax for closures
| From: | ninzya at inbox dot lv | Date: | Wed, 11 Apr 2012 09:38:34 +0000 |
| Subject: | Req #54243 [Com]: Shorter syntax for closures | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-169429@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=54243&edit=1
ID: 54243
Comment by: ninzya at inbox dot lv
Reported by: anil at saog dot net
Summary: Shorter syntax for closures
Status: Open
Type: Feature/Change Request
Package: *General Issues
Operating System: *
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I'm sorry, Anil, but you did not convince me on readability of "Where($($a, $b,
{$a == $b}))".
"Microsoft's way" to define closures in C# linq "Where((a,b) => a ==
b)", in my
opinion, is far more readable. I read it as "a two argument function "(a,b)"
which results ("=>") in a being equal b ("a == b") or whatever the logic is
defined there. However you can not read "$($a, $b, {$a == $b})" as good as you
read microsoft's code. To me, there are too much dollar characters and they make
your eyes hurt when you try to really understand which token does the dollar
sing really belong to. You definately don't want readers of your code to
tokenize a lot when the goal is readability :-)
Previous Comments:
------------------------------------------------------------------------
[2012-03-19 11:54:13] anil at saog dot net
Guys, please calm down. I have no idea why you write these comments in a heat but this issue is just
a wishing. Also, readability is a non-objective property which generally differs person to person,
but the "key" is "shorter means readable". By the way, did you ever inspect c# -
linq syntax?
C# : ...Where((a,b) => a == b)
PHP : ...Where(function($a, $b){ return $a == $b; })
MY : ...Where($($a, $b, {$a == $b}))
readability? yes of course readability... so you think you are better than microsoft on readability?
If you do not agree of course it is okay just tell it (like a human) otherwise keep your ignoble and
invaluable ideas to yourself.
------------------------------------------------------------------------
[2012-03-19 11:13:20] ninzya at inbox dot lv
I think the proposed syntax is not readable at all. Rather it looks like you have
been affected by a "wannabe jQuery inventor". How do you come up with a dollar
sign being "readable"? How do you define "readable code"? If you read your code,
you read it as "dollar, dollar, bracket, dollar, dollar... whatever, dollar". Is
this what you call "readability"?
However I do agree that the "use()" clause of closures sometimes bloats the code a
little bit, especially if the closure is importing a lot of variables from its
context.
------------------------------------------------------------------------
[2012-03-19 09:08:09] danko at very dot lv
Um, no, *that* is unreadable. The original syntax can easily be made readable if you do care:
$myObject->MyMethod(
'abc',
function ($a) use ($b) {
return $a == $b;
}
);
There you go. As readable as it gets - just add some newlines and tabs. On the other hand, a soup of
brackets and dollar signs can't be readable regardless of formatting.
------------------------------------------------------------------------
[2011-03-13 16:16:45] anil at saog dot net
Changed package
------------------------------------------------------------------------
[2011-03-13 16:09:36] anil at saog dot net
Description:
------------
Current closure syntax makes the code a little bit unreadable and also shortening the syntax of a
handy thing like this seems more logical.
Test script:
---------------
As of now "closure" syntax is:
function ($a) use($b){ return $a == $b; }
Passing a closure to any other scope:
$myObject = new myObject ();
$myObject->MyMethod('abc', function ($a) use($b){ return $a == $b; });
Expected result:
----------------
My short syntax candidate is:
$([arg1],[arg2],[arg...], {[method body]}, [scope_var1],[scope_var2],[scope_var...])
So passing with use token syntax:
$myObject = new myObject ();
$myObject->MyMethod('abc', $($a, {$a == $b}, $b));
Without "use" token:
$myObject = new myObject ();
$myObject->MyMethod('abc', $($a, {$a == $b}));
With more than one method argument:
$myObject = new myObject ();
$myObject->MyMethod('abc', $($a,$k,$p,{$a == $b}));
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=54243&edit=1