Stas, On Mon, Aug 31, 2015 at 11:46 PM, Stanislav Malyshev <smalys...@gmail.com> wrote: > Hi! > >> Here it is very obvious that we want to import a variable. Especially, I >> wonder how >> $array = array_map(function ($x) use ($y) { return $x + $y; }, $array); >> is making such simple applications more readable? And especially, what value >> does it add here? > > It is making it more readable by explicitly specifying that we're using > function, that this function imports $y from parent scope and returns $x > + $y. Of course, a seasoned programmer that has years of experience in > functional languages would recognize this pattern, but that's not > exactly the main target audience of PHP.
I think that's quite a bit hyperbolic of an argument. Most programming languages today have a "short form" closure or lambda syntax HackLang: ($x) ==> $x + 1; C++: [](int x) -> int { return x + 1; } Java: (int x) -> x + 1; Python: lambda x: x+1 Ruby: lambda |x| { x + 1 } Rust: |x| x + 1 JavaScript (ES6): x => x + 1 C#: x => x + 1 Objective C: ^(int x) { return x + 1; } Note that not a single one of those is a "functional" language. That's every language in the top 10 index except PERL, PHP and VB.NET (though VB has a similar syntax: function (x as Integer) x + 1), I agree that at first it will feel a little bit weird, especially given that PHP in general lacks syntactic sugar (we still don't have a short-hand to initialize stdclass objects). We introduced [] back in 5.4, and largely it has made readability FAR better (despite some people saying it wouldn't at the time). > And I do not think the argument "you don't have to use it" is a good one > for adding language syntax constructs. If it's in the language, you'd > have to deal with it, and you'd have to teach other people to deal with > it. It's not some function sitting in the extension that may not even be > loaded - it's part of language syntax. So if it would encourage > write-only code, you'd have to deal with it even if you don't personally > create write-only code. I agree 100%. "you don't have to use it" sucks as an argument. However I don't think that's what Bob meant. He was trying to convey that the tool isn't always appropriate in all situations. You can craft code that makes this new syntax look unreadable. But you can craft code that makes this look far more readable. Example: function partial(callable $cb) { return function($left) use ($cb) { return function($right) use ($cb, $left) { return $cb($left, $right); }; }; } vs: function partial(callable $cb) { return $left ~> $right ~> $cb($left, $right); } It may look weird at first, but consider that's because you're not used to the syntax. To me, the top is far harder to follow because of the shear number of tokens to follow, not to mention that I need to pay attention to the use-blocks which may or may not be importing certain variables. The bottom reads exactly how I would expect it to. Both are completely explicit in what they do and how they behave. That's my opinion anyway... Anthony -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php