Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62722 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40053 invoked from network); 3 Sep 2012 11:16:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Sep 2012 11:16:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=derick@php.net; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=derick@php.net; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 82.113.146.227 as permitted sender) X-PHP-List-Original-Sender: derick@php.net X-Host-Fingerprint: 82.113.146.227 xdebug.org Linux 2.6 Received: from [82.113.146.227] ([82.113.146.227:54011] helo=xdebug.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EA/83-20751-59194405 for ; Mon, 03 Sep 2012 07:16:38 -0400 Received: from localhost (localhost [IPv6:::1]) by xdebug.org (Postfix) with ESMTPS id 697C8DE13E; Mon, 3 Sep 2012 12:16:34 +0100 (BST) Date: Mon, 3 Sep 2012 12:16:34 +0100 (BST) X-X-Sender: derick@whisky.home.derickrethans.nl To: Gustavo Lopes cc: Rasmus Lerdorf , Pierre Joye , Stas Malyshev , PHP Internals In-Reply-To: Message-ID: References: <5040DC47.8000305@ajf.me> <5040F4D9.80206@sugarcrm.com> <5042946A.80204@sugarcrm.com> <5042A7D6.7050001@lerdorf.com> User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: Re: [PHP-DEV] Re: Are exceptions allowed in php core? From: derick@php.net (Derick Rethans) On Sun, 2 Sep 2012, Gustavo Lopes wrote: > On Sun, 02 Sep 2012 02:27:02 +0200, Rasmus Lerdorf wrote: > > > $things = getStuff(); > > foreach($things as $thing) { > > do_some($thing); > > } > > > > which is very normal PHP that you see in millions of lines of code, I > > would not expect to have to wrap my foreach in a try/catch here. > > [...] > > > > this is still going to fatal on me eventually with an uncaught exception > > on the foreach if, for example, I pass the generator a second time to > > something and it has run off the end. This is something you don't have > > to worry about currently and I think it will cause headaches for people > > when they start passing generators around. In theory I should be able to > > pass a generator to any existing code that takes an array without having > > to worry about modifying that code to catch foreach exceptions. And yes, > > I know it is actually a generator exception, but to the average user it > > will look like it is foreach throwing. > > I think the first thing anyone who uses generators must understand is that > they are iterators. You forget that not even everybody that uses PHP knows what an iterator is. For all they care, they have an array they can run foreach() on. > They should be compared with iterators, not arrays. They shouldn't be compared with either. They are magical things that happen with a "yield" keyword. > With that in mind, the behavior is not surprising for anyone who knows > how iterators and foreach interact. > > More importantly, there is no other satisfactory solution (except a fatal > error). foreach has no return value, so it has no other way to signal a > failure. If we used a notice or a warning here what would happen is that code > that used generators with an invalid state would, except for the notice, work > as if it had been given an empty iterator. Put another way, it would fail with > only a notice, and continue. Continue doing what? cheers, Derick