Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59368 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68615 invoked from network); 6 Apr 2012 13:59:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Apr 2012 13:59:30 -0000 Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.47 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.47 mail-qa0-f47.google.com Received: from [209.85.216.47] ([209.85.216.47:54421] helo=mail-qa0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 29/E1-57786-1C6FE7F4 for ; Fri, 06 Apr 2012 09:59:30 -0400 Received: by qabg1 with SMTP id g1so468244qab.13 for ; Fri, 06 Apr 2012 06:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=+6QFkkQzEn/BL2rRPBtexkYscvnnDiRpLAWS0SRGLpk=; b=oEcN05B4qAtCLKwGR8fJdiubFRFjnT1K03ywjEotCwYqdE9i2/1XFSR1XbuhBQS6ht J2wQr3WLctseVxgoVd2bEteIU5md9lozyTbqm7Xgb0F5ruE71wY2/Q0fM/X020nls+7u Lzl/km/BnhfloQvwVH/RvEeS9Y4HT/8KZeVRw/1trf2x/A7ptXwCjHc/SuElRcjDBSHS hkBbMFo4m1R5YLqtNPMlffxs6i3k5kZo3HdYlZZF9uNYNT9PMfsJio8o/rzvzjmYWNBK u0Da9VYnrkqQpjJIq++hgKmNFpbP/3wHjf6Tg5C4KJuXgLfifzvgr5V9gZSCJi65El1O RSfg== MIME-Version: 1.0 Received: by 10.229.111.76 with SMTP id r12mr2684626qcp.43.1333720766978; Fri, 06 Apr 2012 06:59:26 -0700 (PDT) Received: by 10.229.42.4 with HTTP; Fri, 6 Apr 2012 06:59:26 -0700 (PDT) In-Reply-To: References: <2FD77C3003DC4656BCF6309B097DD978@gmail.com> Date: Fri, 6 Apr 2012 09:59:26 -0400 Message-ID: To: Rasmus Schultz Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: =?UTF-8?B?UmU6IFtQSFAtREVWXSBSZTog5Zue5aSN77yaIFtQSFAtREVWXSByZXN1bWUgYWZ0ZXIgZQ==?= =?UTF-8?B?eGNlcHRpb24=?= From: ircmaxell@gmail.com (Anthony Ferrara) Rasmus, I think you're missing the difference here. Let's look at an exception. try { doFoo(); throwsException(); doBar(); } catch (Exception $e) { doBaz(); } This is NOT the same as: doFoo(); throwsException('doBaz'); doBar(); To emulate the exception using continuation passing, you'd need to pass two functions, a success and a failure. doFoo(); throwsException('doBaz', 'doBar'); And that's for a single stack level. Imagine how complicated it would become if you were dealing with a large stack or multiple throwers. You'd have something like this: doFoo(); throwsException1(function() { throwsException2(function() { doBar(); }, function() { throwsException3(function() { doBaz(); }, function() { doBiz(); } }); } It'll get really messy really quick. As far as your point about keeping multiple stacks, imagine this code: try { throwsInterrupt(); doSomething(); } catch (Interrupt $i) { callFuncThatInternallyCallsAnotherThatThrowsInterrup(); resume; } Now, you have 3 independent stacks that are raised. The one for the original call to throwsInterrupt, one for the function that internally calls another that throws, and one for the internal interrupt itself. All three would need to be detached at some point from the execution stack and saved for resume. So you're not only unwinding the stacks, but saving them as well incase the interrupt is resumed (to re-wind the stack). Right now, Exceptions don't have this problem, since the stack is never rewound. So it only grows or is unwound based on the exception tree... Not to mention that code execution is always linear in an exception case. It's not possible that code flow will magically jump around. Sure, you could emulate it with goto ( http://codepad.viper-7.com/i3Dhv4 ). But that's explicitly jumping around. Anthony On Fri, Apr 6, 2012 at 9:31 AM, Rasmus Schultz wrote: > 2012/4/5 Anthony Ferrara : >> Why not just do: >> >> function foo(callable $callback) { >> =A0 =A0$a =3D 0; >> =A0 =A0$callback(); >> =A0 =A0$a =3D 1; >> =A0 =A0$callback(); >> } >> >> function bar() { >> =A0 =A0foo(function() { echo 1; }); >> } >> >> It's functionally the same, but doesn't have the stack magic. >> >> Now, it won't be able to do everything that your concept does (bubble >> up to an arbitrary point), but I see that as a good thing, since this >> is explicit. =A0And considering that you're intending to use it as a >> control flow structure (which is not what exceptions are supposed to >> be), I would say exceptions and their dynamic nature would be >> literally a bad thing in this case... > > I don't see how exceptions are anything but a control flow structure? > > If all you wanted was an error-message, you'd be using trigger_error() > - the only way exceptions differ, is that they allow execution to > continue from a certain point, under certain circumstances; it allows > you to control the flow. > >> It's functionally the same, but doesn't have the stack magic. > > your argument and example above is certainly valid, but from the same > point of view, why not get rid of throw/try/catch statements too while > we're at it? > > function foo(callabable $errorhandler) > { > =A0if (some_condition()) { > =A0 =A0$errorhandler(); > =A0} > } > > function bar() { > =A0foo(function() { echo 'an error occurred!'; exit; }); > } > > This works just as well, and as you pointed out, it's probably easier > to understand. > > Now, it won't be able to do everything that an exception does (bubble > up to an arbitrary point), but you could view that as a good thing, > since this is explicit. You could argue that exceptions and their > dynamic nature is literally a bad thing in every case. > > To your technical point: > >> This could get really ugly as you'd be >> forced to have multiple stacks hanging around if you used more than >> one of these in your code. > > I don't see how? > > If you throw an interrupt, and nothing catches it, the program > terminates, same as after an exception. > > if you throw an interrupt and something catches it, that interrupt > (and it's retained stack) only lives for the duration of the > catch-statement: > > try { > =A0... > } catch (Interrupt $i) { > =A0if (some_condition()) > =A0 =A0resume; // (A) > =A0else if (other_condition()) > =A0 =A0throw $i; // (B) > =A0// (C) > } > > There are three ways you can exit this catch{} block: > > (A) we resume execution from the throw-statement, and the previous > stack remains valid. > > (B) the previous stack is preserved for another catch-statement (or > exit with an error-message) > > (C) if we exit the catch{}-block and don't resume, it is safe to > unwind the stack at this point. (assuming we're talking about just > interrupts, and not continuations that can be serialized and resumed > at a later time.) > > I don't know why you think interrupts are so unnatural or difficult to > understand - to me, it would be a natural extension of exceptions. And > I've never understood why they are so frequently compared to GOTO. I > think it's entirely a matter of how you perceive (and apply) the idea > of exceptions - personally I see them not as a "better" replacement > for triggering errors, I really can't see them as anything other than > flow-control statements; there are few cases from which it is really, > truly impossible to recover, but when I identify such a case, I still > use trigger_error() - and granted, this is rare, but there are cases. > > And mind you, registering an error-handler was possible before > exceptions were around, and we got by then - just because something > works or is well-established, doesn't mean there is no room for > improvement. > > I'm not going to pretend I know enough about programming languages to > say for sure that this is a good idea - if this idea has been tried or > researched and proven "bad" already, I'd love to learn about it. But > if so, I doubt it was for any of the reasons I've heard so far... > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >