Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:11669 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42611 invoked by uid 1010); 30 Jul 2004 14:35:45 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 42585 invoked from network); 30 Jul 2004 14:35:45 -0000 Received: from unknown (HELO dns1.systems.townnews.com) (208.229.121.1) by pb1.pair.com with SMTP; 30 Jul 2004 14:35:45 -0000 Received: from [10.81.10.35] (leepcK-165.sub-k.lee.net [12.163.62.165]) (authenticated bits=0) by dns1.systems.townnews.com (8.12.11/8.12.11) with ESMTP id i6UEZm6P022212 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO); Fri, 30 Jul 2004 09:35:48 -0500 Message-ID: <410A5CBC.9090500@townnews.com> Date: Fri, 30 Jul 2004 09:35:40 -0500 User-Agent: Mozilla Thunderbird 0.7.1 (Windows/20040626) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Paul G CC: internals@lists.php.net References: <20040729021337.73894.qmail@pb1.pair.com> <200407291117.09573.lpedretti@suserver.com> <200407291037.12396.ilia@prohost.org> <41098F91.3000606@hristov.com> <004801c475bd$812f3580$0200a8c0@rusko> In-Reply-To: <004801c475bd$812f3580$0200a8c0@rusko> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] GOTO operator From: sthomas@townnews.com (Shaun Thomas) Paul G wrote: > function pseudocode() > { > $a=allocate_resource_z(); > $b=allocate_resource_y(); > > $res=do_stuff(); > if(!$res) > goto err_out; > > $c=allocate_resource_x(); > $res=do_more_stuff(); > if(!$res) > goto err_out; > > $d=allocate_resource_foo(); > $res=do_even_more_stuff(); > if(!$res) > goto err_out; > > return true; > > err_out: > free_resouce($a); > free_resouce($b); > if(isset($c)) free_resouce($c); > if(isset($d)) free_resouce($c); > > return false; > } While this is valid, it's not really a good example of pseudocode that requires a goto to be efficient. You could do the same thing with try/catch/throw: function pseudocode() { $a=allocate_resource_z(); $b=allocate_resource_y(); try { $res=do_stuff(); if(!$res) throw SOME_ERROR; $c=allocate_resource_x(); $res=do_more_stuff(); if(!$res) throw SOME_ERROR; $d=allocate_resource_foo(); $res=do_even_more_stuff(); if(!$res) throw SOME_ERROR; return true; } catch SOME_ERROR { free_resouce($a); free_resouce($b); if(isset($c)) free_resouce($c); if(isset($d)) free_resouce($c); return false; } } This is also one of the reasons languages like Ada have named code blocks for exceptions. Here's some ada-like pseudo-code: BEGIN a := allocate_resource_z(); b := allocate_resource_y(); res := do_stuff(); if (!res) raise some_exception; c := allocate_resource_x(); res := =do_more_stuff(); if (!res) raise some_exception; d := allocate_resource_foo(); res := do_even_more_stuff(); if(!res) raise unknown; return TRUE; EXCEPTION free_resouce(a); free_resouce(b); if (c) free_resouce(c); if (d) free_resouce(c); return FALSE; END The case that Ilia and the other main developers is not for cases like this. If you look through the Linux kernel, you can see very good examples of goto usage that is not related to this kind of error handling. It's mainly to jump around inside parsing code while maintaining a stack, so the same chunk of code can continuously handle the same source of input without hundreds of if statements and indented blocks. -- Shaun M. Thomas INN Database Administrator Phone: (309) 743-0812 Fax : (309) 743-0830 Email: sthomas@townnews.com Web : www.townnews.com