Re: [RFC] [Discussion] Short Closures

From: Date: Mon, 07 Sep 2015 19:05:29 +0000
Subject: Re: [RFC] [Discussion] Short Closures
References: 1 2 3 4 5 6 7 8 9 10 11 12 13  Groups: php.internals 
Request: Send a blank email to internals+get-88108@lists.php.net to get a copy of this message
> I think a possible improvement might be a generalised syntax, similar to > that used for constants, that lets you use any \Closure object to define a > function or method. Thus: > > function reduce = $initial ~> $fn ~> $input ~> { > // ... > }; I like this but we're getting a bit too far in the future RFC territory. But bringing this back to the current proposal I think that the curly brace syntax should stay. I much prefer this: function reduce = $initial ~> $fn ~> $input ~> { $accumulator = $initial; foreach ($input as $value) { $accumulator = $fn($accumulator, $value); } return $accumulator; }; To this: function reduce = $initial ~> $fn ~> function($input) use($initial, $fn) { $accumulator = $initial; foreach ($input as $value) { $accumulator = $fn($accumulator, $value); } return $accumulator; }; So while assigning the result to a named function (instead of a variable) is out of scope of this RFC I think it demonstrates the value of the block syntax which I think we should keep. I've said this several times now, so I won't say it again :) > This has that advantage that it isn't just useful for when you want to use > the short syntax. You can also use it when you're obtaining a Closure in > some novel way, perhaps a higher-order function: > > // where "add" is a function that adds two numbers > function sum = reduce(0)("add"); > > With this, you don't have to write a wrapper function, or use a global > variable, to define a proper top-level function. I like this as well but again we're getting a bit too far away from the current proposal.

« previous php.internals (#88108) next »