Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101156 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99833 invoked from network); 27 Nov 2017 04:06:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Nov 2017 04:06:44 -0000 Authentication-Results: pb1.pair.com smtp.mail=enclaved@Safe-mail.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=enclaved@Safe-mail.net; sender-id=pass; domainkeys=good Received-SPF: pass (pb1.pair.com: domain Safe-mail.net designates 212.29.227.83 as permitted sender) DomainKey-Status: good X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: enclaved@Safe-mail.net X-Host-Fingerprint: 212.29.227.83 mango.safe-mail.net Received: from [212.29.227.83] ([212.29.227.83:55478] helo=mango.safe-mail.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E9/94-26862-25F8B1A5 for ; Sun, 26 Nov 2017 23:06:42 -0500 Received: by mango.safe-mail.net with Safe-mail (Exim 4.84) (envelope-from ) id 1eJAgp-0003cS-BO for internals@lists.php.net; Sun, 26 Nov 2017 23:06:39 -0500 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=N1-0105; d=Safe-mail.net; b=h1Ho0zKJqpO7/8lGeusV1ABoYyCYWcd0XWz8DQL5R55Z3KRy4ereA1+YuWrpSegw gTZPRi/Wjw7RUItvlslHjq8gL+LG5yzOm0oGWS6h52xtbRMT7UThGArJG7l1Xy4o UdBaixdAgthZ2xWpkiKPbq12HqCyR/LuzxgjnJUgp/I=; Received: from pc ([109.63.246.156]) by Safe-mail.net with https Date: Mon, 27 Nov 2017 07:06:39 +0300 To: internals@lists.php.net X-SMType: Regular X-SMRef: N1M-7phdB5if5V Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-SMSignature: Bs/hwFqUGq7B9pG4ix7yMXHXvpD7tMpaSKyDgVJ/dKQHGyur08pwaZEpgoy8m6eH 43YxqhSJd3gxWfIturXBIQuiUBrWEUs0bzhF1kEqzSzj32NjCorbDSuWQOf9gZKx 6GIgcd/eRAEMIPkT26Pg39DK2V2unFi3c+yuwzcuz6w= Subject: C++-like function-try-block proposal From: enclaved@Safe-mail.net Greetings! I have been referred to this mailing list by https://wiki.php.net/rfc/howto as the first step to propose a language change. I first tried to register on wiki.php.net as per php-src/README.SUBMITTING_PATCH in order to actually make an RFC, only to be given an error: Writing /srv/web-wiki/dokuwiki/conf/users.auth.php failed I am proposing a patch (see attachment) to bring C++-like function-try-block shorthand syntax (http://en.cppreference.com/w/cpp/language/function-try-block) for whole-function try-catch-finally blocks: function fuu($foo, $bar): void try { may_throw($foo); may_throw($bar); } catch (FuuException $e) { echo $e->getMessage(); } Since PHP is partially influenced by C++, I believe this syntax would be a meaningful step in making PHP a little bit more intuitively compatible with C++ practices, which may prove useful for people who come to PHP from the C++ world (such as myself.) The expected benefit is PHP becomes yet more familiar and comfortable for C++ programmers. Also, this syntax saves us one set of curly braces and an indentation level, allowing for more compact and expressive functions, and especially closures: $s = preg_replace($re, $s, function (array $m): void try { return find_replacement(...$m); } catch (Throwable $e) { return $m[0]; }); In addition to the syntactic sugar, I would like to outline another possible merit: the very fact of such a shorthand construct existing may encourage programmers to write functions with a consistent approach to exception handling at whole-function level. Each such function becomes a self-contained code unit tightly coupled with its exception handling logic, the programmer's intent clearly outlined by the use of the proposed construct, thereby facilitating code clarity and integrity and promoting a stricter and more rigid code style. The proposed patch makes purely cosmetic modifications to zend_language_parser.y grammar to augment plain functions, class methods, and closures to accept try-catch-finally blocks as their bodies in addition to regular compound statements. It amounts to just a handful of lines and entails no API changes. I submitted my patch via the bugtracker: https://bugs.php.net/bug.php?id=75576 Please share your thoughts. --Alexei Gerasimov