On Thu, Jul 19, 2012 at 10:58 AM, Charlie Somerville <char...@charliesomerville.com> 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 with > 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 group > 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 example > of its use: > > some_var = 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 often > 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 statements > 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 > <ivan.ender...@hoa-project.net> 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 “feature” (uniformity > 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 > > -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php