php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54243 Shorter syntax for closures
Submitted: 2011-03-13 16:09 UTC Modified: 2016-06-17 18:01 UTC
Votes:5
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:4 (100.0%)
Same OS:4 (100.0%)
From: anil at saog dot net Assigned:
Status: Suspended Package: *General Issues
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: anil at saog dot net
New email:
PHP Version: OS:

 

 [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}));


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-13 16:16 UTC] anil at saog dot net
-Package: Scripting Engine problem +Package: *General Issues -Operating System: +Operating System: *
 [2011-03-13 16:16 UTC] anil at saog dot net
Changed package
 [2012-03-19 09:08 UTC] 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.
 [2012-03-19 11:13 UTC] 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 11:54 UTC] 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-04-11 09:38 UTC] ninzya at inbox dot lv
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 :-)
 [2012-04-11 14:55 UTC] johannes@php.net
-Status: Open +Status: Suspended
 [2012-04-11 14:55 UTC] johannes@php.net
One of the goals of PHP is to have a "verbose enough" syntax. Giving people ways to search for information if they don't understand it. With the current syntax people reading the code see the word "function" an can get an idea what this might be and have a term to put in a search engine.

On the other side sometimes that is changed, like with arrays, initially one had to use array() to build arrays, nowadays we have the $a = [ ]; as shortcut, so maybe we'll add a new syntax, but only in the long run, when we see that anonymous functions really are going mainstream.
 [2012-12-03 04:01 UTC] id2639730 at gmail dot com
When do you plan to implement short syntax for closures?
+1 for c# lambda style. 
Ecmascript 6 short function syntax used this approach. Typescript language already using lambdas.
Zend Framework 2 and Symfony2 actively using closures to work with events.
 [2016-06-17 18:01 UTC] cmb@php.net
Such change would require an RFC anyway. Anybody is free to write
one, see <https://wiki.php.net/rfc/howto>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 01:01:28 2024 UTC