7 ms·
PHP gets Generators
- mgkimsal 14y agoInteresting - only one vote against (final numbers were 24-1).
- TazeTSchnitzel 14y agoI find it amusing that, on the PHP internals mailing list, RFCs will be heavily "debated" (i.e., argued) about, and you'd think from reading it everyone hates something. Then it comes to vote and >90% vote for it, usually :)
- dpcx 14y agoEverything should be debated. It ensures that you get the best possible product. We may agree on something, but if no opposing views are given, how do we know it's the best?
- TazeTSchnitzel 14y agoI'm not opposing debate. You'd understand what I was on about if you read the PHP internals mailing list - the same points are argued to death; instead of a good discussion, it becomes a huge argument.
- smsm42 14y agoYou misunderstand how the list works :) If people discuss small details of it passionately, that means they care about the big concept enough that they want to get the details right. If they hated the whole idea, most people would just say "it doesn't make sense" and move on, and there would be 2-3 people discussing something irrelevant until the thread dies off. That happens too, but arguing about a feature doesn't mean people hate it. More often than not it means people are interested in it.
- wccrawford 14y agoDebate isn't always about arguing your side. Sometimes it's about making sure all the sides are argued so that you can know you're going the right direction.
- antirez 14y agoPHP is the perfect showcase of how good languages are not sum of features at all. I don't think you can fix PHP just adding stuff that it lacks. It can be improved in this way as, at least, it is possible for a good programmer to pick a subset of features that makes the language less a pain to use, but I wish PHP could start changing in a "let's redesign it" way. Disclaimer: I'm a big fan of PHP, not because I like the language, but because I like the pragmatic approach.
- TazeTSchnitzel 14y agoPHP does make big, backwards-incompatible changes at major versions. They are slowly happening, but only slowly, unfortunately. But I hope to see some big changes before trunk becomes PHP 6.
- encoderer 14y agoI'll bite... like what? All of PHP4's syntax is still valid in PHP5 even if it does raise a deprecated notice. The only real thing that broke in the upgrade is if you had a LOC that was expecting an object to be passed by value. Theoretically possible but practically very very rare. And of course config settings like register globals being changed, but that's just that: a config change.
- TazeTSchnitzel 14y agoregister_globals was removed. PHP5 changed the semantics of objects entirely. Before they were value types. Still, yes, not enough has changed IMO.
- encoderer 14y agoThis is certainly a debate about semantics, but I don't think you mean "semantics." They changed the implementation of objects. And they added new syntax. But they made no changes that affected the old syntax -- except the one I mentioned.
- Kilimanjaro 14y agothe/worst/php/travesti/is/namespaces
- jasonlotito 14y agoI hated \ as well when I first heard about it, but frankly, after actually working with it, it doesn't matter one bit.
- wvenable 14y agoBackslash. And honestly, it's really not bad.
- soulclap 14y agoNot getting the problem either, working with PHP namespaces daily.
- MindTwister 14y agoYou\mean\travesty\obviously
- TazeTSchnitzel 14y agoThey couldn't have made it a forward slash and you damn well know that.
- Jebus 14y agoYeah, stop adding features, it's making <my language of choice> look bad! [/sarcasm]
- agumonkey 14y agoI used to say that PHP tried to be Java. Closures, traits and now generators, linguistically it's getting ahead.
- TazeTSchnitzel 14y agoPHP is still very Java-like in some ways though. Well... PHP likes to pretend it's a dynamic language (Closures, for instance), but at its core it's a very strange dynamic/static hybrid, utilising a single-pass compiler and with type checking done with syntax definitions(!) Which is why this doesn't work: (actual intended production code) self::$views[$path](); Why? Because the syntax doesn't accomodate calling a closure that is an item in an array, which is a static member of a class. But this works: $x = self::$views[$path]; $x();
- SeoxyS 14y agoThis feature seems seriously contrived and they could have tried to learn a thing or two from ruby here. The idea that the code flow is now no longer linear, and that the programmer now has to understand perfectly the odd rules by which the execution of code now just jumps around… boggles the mind. A closure-based approach would have been much more semantically sane; if a little syntactically ugly. But then again, how hard would it have been to add a nicer closure syntax to PHP, instead of creating this mess of a feature? Take this: def each_line_in_file f = File.open('hello.txt', 'r') while line = f.gets yield line end f.close end each_line_in_file do |l| puts l end This is a real-world Ruby example of the feature. The semantics here are clear, each_line is a higher-order function which takes a closure as an arguments, and calls it (yields to it) for each line in the file. Ruby allows the programmer to use the yield keyword to avoid having to declare the closure as an argument (`def each_line(&block)`). The same can even be implemented in PHP as it stands today, although the syntax is quite unsightly: function each_line_in_file($callback) { $f = fopen('hello.txt', 'r'); while (null !== ($line = fgets($f))) { $callback($line); } fclose(f); } each_line_in_file(function ($l) { echo $l."\n"; });
- masklinn 14y ago> This feature seems seriously contrived and they could have tried to learn a thing or two from ruby here. That's way too late, PHP is an imperatively styled and statements-based language, it uses iterables and external iteration, not internal. That's what the language is. > The semantics here are clear There's nothing unclear about generator semantics, regardless of your refusal to understand them. > The same can even be implemented in PHP as it stands today Not quite, PHP doesn't have non-local returns, so you soon get into annoying issues of not being able to break away from an iteration (or of having an utterly terrible API to do so, as in [NSArray enumerateObjectsUsingBlock:] > Ruby allows the programmer to use the yield keyword to avoid having to declare the closure as an argument (`def each_line(&block)`). So you dislike one magic but you like an other for pretty much no reason at all beyond knowing one and not the other? Consistency not your style?
- 14y ago
- mappu 14y agoI'm not quite caught up on the mailing list - but it looks like the last-minute point about traversing an already-exhausted generator (see [1]) has held. That's really the most important thing for developers to notice - whether you use the feature often or not, you must be aware that foreach() might throw, not just emit an E_WARNING. ____________________ 1. https://wiki.php.net/rfc/generators#rewinding_a_generator https://wiki.php.net/rfc/generators#rewinding_a_generator
- TazeTSchnitzel 14y agoI guess you'll be upset then that I, personally, was for it using Exceptions, because Generators are Iterators.
- mappu 14y agoIt's a sensible choice and i understand the reason for it being this way. It does seem like it's exposing an implementation detail, though, and out of the rest of the core language syntax, nothing else throws an exception. I'm happy to see the feature, but this is one of those quirks that, in another five years, will end up in the next "A Fractal Of Bad Design" blog post.
- krob 14y agoI think probably the biggest hurdle for a lot of devs is really a fix on naming conventions and argument order. I think this could be accomplished with a large aliased namespace. maybe something like use \stdlib as _; _\array_walk(<array>,<callback>) _\array_map(<array>,<callback>) _\array_reduce(<array>,<callback>) ...