Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61468 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41968 invoked from network); 19 Jul 2012 10:03:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jul 2012 10:03:11 -0000 Authentication-Results: pb1.pair.com header.from=dragoonis@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=dragoonis@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.170 as permitted sender) X-PHP-List-Original-Sender: dragoonis@gmail.com X-Host-Fingerprint: 209.85.213.170 mail-yx0-f170.google.com Received: from [209.85.213.170] ([209.85.213.170:42687] helo=mail-yx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A8/03-25752-E5BD7005 for ; Thu, 19 Jul 2012 06:03:10 -0400 Received: by yenl12 with SMTP id l12so2927187yen.29 for ; Thu, 19 Jul 2012 03:03:07 -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=8nXNZaEw4Rb4tg6vAXP+AsG0rf1cpGgivgkYEYoxZlw=; b=NxaQSLOutGQ/6TwozjytuKmkhLO3YUQexV5UVRH88exC4wFzYdv4YSlWHVRmFo3Vh4 aZclV2FDSzKYXqZis3asTxO3uxG5pZTFumc0hzQOZeLE1VpFwSpe1UMQ0J5HFJY+dpd7 aN65KuBT9+nzi1O8ACmWhSEV23VfCWOs2I6hVCa0EUB9SSRAMz8DCbKsFqzljD/sJVg3 jEL6R4ZAcAmpmZhr2iZS42Y+CXanpIad0+FO/h3paFD0Bs24+UGEOxgy0kbKWbmcAdAy zaY+9WHjBvmeDESAxyjqkxjYE7g5jSJYRq4V89LN35YB5KBJe4O23YB68qgWEoQWwTj6 5oqQ== MIME-Version: 1.0 Received: by 10.50.216.202 with SMTP id os10mr4642504igc.17.1342692187369; Thu, 19 Jul 2012 03:03:07 -0700 (PDT) Received: by 10.64.32.72 with HTTP; Thu, 19 Jul 2012 03:03:07 -0700 (PDT) In-Reply-To: References: <5007D707.2070100@hoa-project.net> Date: Thu, 19 Jul 2012 11:03:07 +0100 Message-ID: To: Charlie Somerville Cc: internals@lists.php.net Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Make try/catch brackets optinal From: dragoonis@gmail.com (Paul Dragoonis) On Thu, Jul 19, 2012 at 10:58 AM, Charlie Somerville wrote: > On Thursday, 19 July 2012 at 7:49 PM, Paul Dragoonis wrote: > > Why is your try block only going to contain 1 line, and that's > throwing an exception?? > > try > throw new Exception('foobar'); > catch(Exception $e) > > Because it's a contrived example. He's not trying to write real code, he'= s > trying to demonstrate his point - and you totally missed that You're right, I totally missed that point. > > Braces are a good thing, they give structure and stop people from > mis-reading things and writing bugs, the same can be said for the if() > situation. > > 1) Braces are good. > > This is subjective. There are some cases where it might improve code > readability to drop the braces for a single-statement try/catch. > > There's certainly no technical barrier to doing this. I'm not familiar wi= th > PHP's parser, but I'd imagine there would be some kind of 'statement' > non-terminal that would handle single statements as well as a braced grou= p > of statements. Same sentiments as from Rafael, bracket-less is bug-prone. > > 2) Try with only one line in it to throw an exception doesn't seem > like a realistic situation. > > There could be some utility to this. For example, as well as having post-= fix > if, unless, etc., Ruby also has a post-fix 'rescue'. Here's a silly examp= le > of its use: > > some_var =3D foo.bar rescue "oops" > > If 'foo.bar' threw an exception, some_var would contain "oops" instead. > > I think PHP could benefit from having a single statement try form. I ofte= n > turn to PHP for quick and dirty scripts when I need to do something with > little fuss. I think having try/catch support brace-less single statement= s > would help increase consistency in PHP's syntax, as well as be useful in > certain situations. > > On Thursday, 19 July 2012 at 7:49 PM, Paul Dragoonis wrote: > > > -1 from me, sorry Hoa. > > On Thu, Jul 19, 2012 at 10:44 AM, Ivan Enderlin @ Hoa > wrote: > > Hi internals, > > As you certainly know, brackets defining blocks in PHP are optional if > blocks contain a single instruction. Thus: > > if($condition) { > echo 'foobar'; > } > > is strictly equivalent to: > > if($condition) > echo 'foobar'; > > But this syntactic sugar is not applied uniformly to all PHP language > constructions. I have the try/catch couple in mind. > First, I would like to know why it is not possible to write: > > try > throw new Exception('foobar'); > catch(Exception $e) > var_dump($e->getMessage()); > > as a strict equivalence of: > > try { > throw new Exception('foobar'); > } > catch(Exception $e) { > var_dump($e->getMessage()); > } > > Second, if it is possible, could we plan to have this =93feature=94 (unif= ormity > actually) in PHP6 (or maybe before)? > > Best regards. > > -- > Ivan Enderlin > Developer of Hoa > http://hoa.42/ or http://hoa-project.net/ > > PhD. student at DISC/Femto-ST (Vesontio) and INRIA (Cassis) > http://disc.univ-fcomte.fr/ and http://www.inria.fr/ > > Member of HTML and WebApps Working Group of W3C > http://w3.org/ > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >