Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101157 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7640 invoked from network); 27 Nov 2017 06:53:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Nov 2017 06:53:08 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.52 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.52 mail-it0-f52.google.com Received: from [209.85.214.52] ([209.85.214.52:38677] helo=mail-it0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3C/35-26862-056BB1A5 for ; Mon, 27 Nov 2017 01:53:04 -0500 Received: by mail-it0-f52.google.com with SMTP id n134so19983700itg.3 for ; Sun, 26 Nov 2017 22:53:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=chRxANZmVc/MWbnWezHzI7OpD5hepqPhwNT3539VtmQ=; b=DPi8ERdmmm8NzcLZL20SUmIjUWifNd9XK0PJKb5AYgKhN58QtI+K4jiG1/CiiqENyx re73/oQI1rUIke0csOj7CXQBeRyi4JghpOOkaFB7KxTl8ZimGV2vQ8M3zArrGy06cnUQ Qi0+sgo8p4i4RabAFdwyX/sfQ0FBtV7/tvJv9m+/NR8HJQDM+HR8yCKn/8RwnmyShoKT moge3qoRjxSdv+DKa8IScSoVWTKxwY01aGq0/jV0jFVtUsOwxtNBvr3+F5egLEn6Q54g Qz1TYSvCeCNW9lM8CzMciuCwA58TiTI1CHxsdxWuOQNwE2MDxUIvPuubaN4xfMzcjafs mDyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=chRxANZmVc/MWbnWezHzI7OpD5hepqPhwNT3539VtmQ=; b=OcHjWxyJp00KsRlZAbd3EEo468WGZGW54v6S4yZThC3KA4wMNWgqQer6mzBTQStqEQ KAVwmInIa/OC1n6t3sI1Nen/j/ZYGaKS0YRMrxKjWBR2JdSPoNdxbNLtrKd6AkWDpxFZ ASwOOHOmK1DUpFq0BeHjSACjYEwJtHAwXLABXWkgmy9Ldt94fhc+uQJYbwzDdA0tqKEC 14GWpucP4DZPIRnW2hCctt3tMWGb4SGD5s+hIhVJdhur0KuNmaTDd2Tf7vuVNTOXqzsQ rxSU2v2YBspRdCvdmpZ5XT9jIU9s5scMxhJhE/VggDe/ToMpiEwRPlAOnmlqy8sDamKR 4FgA== X-Gm-Message-State: AJaThX6EVKOOTT8Q98dc2jp6LuIobZEmxNfRjdZN/NzI2xxBEizjIQUV Skvu39V4uvDp6jcBXkvo2h3lMdtVNyxYnqXLxM4= X-Google-Smtp-Source: AGs4zMZKxMLnHSCZNn176vwxPjdOiWuGUzWX/l78Z2CsvPl9OG8wpiPZBKTibf3dpZF58u7Gi8WWXF+dJRMHKbDHSaI= X-Received: by 10.36.44.197 with SMTP id i188mr26978340iti.40.1511765581562; Sun, 26 Nov 2017 22:53:01 -0800 (PST) MIME-Version: 1.0 Received: by 10.107.138.93 with HTTP; Sun, 26 Nov 2017 22:53:01 -0800 (PST) In-Reply-To: References: Date: Mon, 27 Nov 2017 07:53:01 +0100 Message-ID: To: enclaved@safe-mail.net Cc: PHP internals Content-Type: multipart/alternative; boundary="001a1143d3307907cb055ef15a57" Subject: Re: [PHP-DEV] C++-like function-try-block proposal From: nikita.ppv@gmail.com (Nikita Popov) --001a1143d3307907cb055ef15a57 Content-Type: text/plain; charset="UTF-8" On Mon, Nov 27, 2017 at 5:06 AM, wrote: > 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. > I believe that the reason why C++ has function-level try-catch blocks is that they provide the only way of catching exceptions thrown by the member initializer list of a constructor. As PHP does not have any such functionality, PHP has no need for function-level try-catch blocks. I doubt that C++ introduced these as a way to save a couple of braces, especially given that C++ does not allow the use of other, and arguably significantly more common, control-flow statements without an explicit function block. Nikita --001a1143d3307907cb055ef15a57--