|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-03-13 16:09 UTC] 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}));
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 03:00:01 2025 UTC |
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.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.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 :-)