Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62643 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 89877 invoked from network); 2 Sep 2012 00:43:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Sep 2012 00:43:59 -0000 Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.21 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.21 smtp1.ist.utl.pt Linux 2.6 Received: from [193.136.128.21] ([193.136.128.21:52804] helo=smtp1.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A6/EB-17065-ECBA2405 for ; Sat, 01 Sep 2012 20:43:59 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp1.ist.utl.pt (Postfix) with ESMTP id 76F9D70003C3; Sun, 2 Sep 2012 01:43:55 +0100 (WEST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp1.ist.utl.pt ([127.0.0.1]) by localhost (smtp1.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id qTQgiux0uyCx; Sun, 2 Sep 2012 01:43:55 +0100 (WEST) Received: from mail2.ist.utl.pt (mail.ist.utl.pt [IPv6:2001:690:2100:1::8]) by smtp1.ist.utl.pt (Postfix) with ESMTP id BB3847000451; Sun, 2 Sep 2012 01:43:54 +0100 (WEST) Received: from damnation.nl.lo.geleia.net (unknown [IPv6:2001:470:94a2:4:21d:baff:feee:cc0b]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id 6E3232008494; Sun, 2 Sep 2012 01:43:53 +0100 (WEST) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: "Rasmus Lerdorf" Cc: "Pierre Joye" , "Stas Malyshev" , "PHP Internals" References: <5040DC47.8000305@ajf.me> <5040F4D9.80206@sugarcrm.com> <5042946A.80204@sugarcrm.com> <5042A7D6.7050001@lerdorf.com> Date: Sun, 02 Sep 2012 02:44:08 +0200 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Organization: =?utf-8?Q?N=C3=BAcleo_de_Eng=2E_Biom=C3=A9di?= =?utf-8?Q?ca_do_I=2ES=2ET=2E?= Message-ID: In-Reply-To: <5042A7D6.7050001@lerdorf.com> User-Agent: Opera Mail/12.01 (Linux) Subject: Re: [PHP-DEV] Re: Are exceptions allowed in php core? From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Sun, 02 Sep 2012 02:27:02 +0200, Rasmus Lerdorf wrote: > On 09/01/2012 04:51 PM, Gustavo Lopes wrote > >> * In fact, if there is a unifying theme in the usage of exceptions in >> PHP, is that exceptions are used when OOP interfaces are involved (see >> Zend interfaces, SPL, DateTime and PDO -- though optional there). The >> core vs. non-core argument only looks attractive because there are few >> built-in classes in the core. > > I think this is drifting off-topic. Whether or not a generator is an > object is an implementation detail. My message is certainly on-topic for this thread. But you are right in that correct line of inquiry for the generator error condition is whether an exception/fatal error is appropriate in this situation, not whether exceptions are allowed in the core. For the reasons I explain next, I think it is. > $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. They should be compared with iterators, not arrays. 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. That is a far more serious problem. In other circumstances where we emit warnings, we usually have a return value the caller can check for error conditions. -- Gustavo Lopes