Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] C++-like function-try-block proposal

Posted by Anonymous User 
Anonymous User
[PHP-DEV] C++-like function-try-block proposal
November 27, 2017 05:10AM
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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Nikita Popov
Re: [PHP-DEV] C++-like function-try-block proposal
November 27, 2017 08:00AM
On Mon, Nov 27, 2017 at 5:06 AM, <[email protected]> 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
Niklas Keller
Re: [PHP-DEV] C++-like function-try-block proposal
November 27, 2017 09:10AM
Hey,


> 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
>

Should be fixed now, please retry.

Regards, Niklas
Sara Golemon
Re: [PHP-DEV] C++-like function-try-block proposal
November 27, 2017 04:10PM
On Sun, Nov 26, 2017 at 11:06 PM, <[email protected]> wrote:
> I am proposing a patch (see attachment)
>
php.net mailing lists don't like attachments (virus paranoia and
similar). If you have a patch, the best thing to do is submit it as a
PR on github. Then, we can all link to parts and add comments and
it's generally a better place to discuss implementation.

> 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:
>
It's a pretty minor bit of syntactic sugar that I use in C++
occasionally and it can reduce cognitive overhead by some fraction of
a percent at the cost of a little extra complexity in the parser and
no compiler/runtime changes. Overall I'm only +0 on it as it does add
complexity to the parser for a very very tiny benefit.

Consider the following already legal syntax:

function fuu($foo, $bar): void {try
{
may_throw($foo);
may_throw($bar);
} catch (FuuException $e) {
echo $e->getMessage();
}}

Same effect and very nearly same visual scan as the block you pasted,
but with no need for additional syntax.

> 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.
>
I'm not sure this statement is entirely accurate. PHP's fundamental
inspirations come from perl and C. It's OOP layer was inspired in
part from C++, but also from Java. This mixed ancestry doesn't
necessarily indicate a push toward any one of those syntaxes. PHP is
PHP, it's best when it steals the parts of other languages which make
sense for it to steal.

> I submitted my patch via the bugtracker: https://bugs.php.net/bug.php?id=75576
>
Ah, disregard my initial comment above then (though PRs do have advantages)..
At a glance, your patch makes sense.
Alexei Gerasimov
Re: [PHP-DEV] C++-like function-try-block proposal
November 27, 2017 07:30PM
> Should be fixed now, please retry.

Yes, this time it worked, thank you. I'd like to go ahead and write a better-phrased
RFC out of what feedback I have gotten so far. The RFC howto speaks of some "karma".
How do I get that?

-Alexei

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Alexei Gerasimov
Re: [PHP-DEV] C++-like function-try-block proposal
November 27, 2017 07:50PM
> > 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.
> >
> I'm not sure this statement is entirely accurate. PHP's fundamental
> inspirations come from perl and C. It's OOP layer was inspired in
> part from C++, but also from Java.

Oh, I didn't mean to single C++ out, hence "partially". When I started with PHP OOP not too long ago, I kinda expected that function-try-block construct to work, for no real reason other than a hunch, and it didn't. So after taking a look at the parser, I figured it wouldn't be a big deal to add it just as syntactic sugar (Nikita pointed it out correctly that in C++ it was introduced for the purpose of wrapping around initalizer lists.) After all it'd cost us just one additional level of yacc grammar, i.e. nothing to worry about performance-wise.

I could try to come up with a better and more realistic use case example for it if I am ever allowed to make an actual RFC out of it. But still, perhaps my best argument is that it is very cheap to implement, and may potentially make code look neater.

-Alexei

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
Re: [PHP-DEV] C++-like function-try-block proposal
November 27, 2017 08:30PM
On 27.11.2017 at 19:23, wrote Alexei Gerasimov:

> I'd like to go ahead and write a better-phrased
> RFC out of what feedback I have gotten so far. The RFC howto speaks of some "karma".
> How do I get that?

You have to register for a Wiki account first, see
https://wiki.php.net/rfc?do=register. Then someone has to approve it.

--
Christoph M. Becker


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Alexei Gerasimov
Re: [PHP-DEV] C++-like function-try-block proposal
November 27, 2017 08:40PM
> > I'd like to go ahead and write a better-phrased
> > RFC out of what feedback I have gotten so far. The RFC howto speaks of some "karma".
> > How do I get that?
>
> You have to register for a Wiki account first, see
> https://wiki.php.net/rfc?do=register. Then someone has to approve it.

I did (using this e-mail address), and got the message: "The user has been created and the password was sent by email." However, I did not receive any e-mail with the password.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Niklas Keller
Re: [PHP-DEV] C++-like function-try-block proposal
November 27, 2017 09:40PM
Hey Alexei,


> I did (using this e-mail address), and got the message: "The user has been
> created and the password was sent by email." However, I did not receive any
> e-mail with the password.
>

Sorry for the trouble, seems like nobody tested the sign up process after
the server migration yet. It seems like the e-mail sender configuration is
missing.

Unfortunately, I don't know the right config, so I can't simply fix it, but
I've sent a message to Hannes, he probably knows.

Regards, Niklas
Alexei Gerasimov
Re: [PHP-DEV] C++-like function-try-block proposal
December 02, 2017 02:30PM
> > I did (using this e-mail address), and got the message: "The user has been created and the password was sent by email." However, I did not receive any e-mail with the password.
>
> Sorry for the trouble, seems like nobody tested the sign up process after the server migration yet. It seems like the e-mail sender configuration is missing.
>
>
> Unfortunately, I don't know the right config, so I can't simply fix it, but I've sent a message to Hannes, he probably knows.

Any update? The reset password feature of wiki.php.net doesn't work either, I receive no e-mail.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Andreas Hennings
Re: [PHP-DEV] C++-like function-try-block proposal
December 02, 2017 07:20PM
So to summarize, this feature would save us brackets and one indentation
level.
Saving an indentation level can be quite useful to reduce a git commit
footprint.

On the other hand, it might confuse some people. It would blur the concept
of a function body scope, and thus the mental model of how the language
works.
E.g. are local variables and parameters shared between the try {} and the
catch {} block?

It would also mean that userland PHP parsers and IDEs need to support the
syntax.

So personally I am undecided or skeptical. It does feel alien to me.
Usually if I want to avoid the extra indentation, I will put the try {}
part into a separate function.
But this is just me.


On 2 December 2017 at 14:27, Alexei Gerasimov <[email protected]>
wrote:

> > > I did (using this e-mail address), and got the message: "The user has
> been created and the password was sent by email." However, I did not
> receive any e-mail with the password.
> >
> > Sorry for the trouble, seems like nobody tested the sign up process
> after the server migration yet. It seems like the e-mail sender
> configuration is missing.
> >
> >
> > Unfortunately, I don't know the right config, so I can't simply fix it,
> but I've sent a message to Hannes, he probably knows.
>
> Any update? The reset password feature of wiki.php.net doesn't work
> either, I receive no e-mail.
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Sherif Ramadan
Re: [PHP-DEV] C++-like function-try-block proposal
December 02, 2017 09:10PM
I think we get this "I want to make PHP more like <language X>" thing more
often than any other language change proposals. I'm not strictly opposed to
the idea of borrowing from other languages. Though when the thesis
statement is almost exclusively about making PHP feel more like <language
X> than anything else I'd rather not entertain the idea further.

I agree with Nikita that this feature in C++ adds no intrinsic value to
PHP. I'm never overly excited about purely syntactic sugar changes in PHP.
We've already changed so much of the language in PHP 7 that I'm finding
some people struggling with keeping up with PHP 5 - 7 migration paths (and
I'm speaking from first hand experience having to deal with one myself at
work).

I'd like to see an RFC that demonstrates a stronger use case than "avoiding
one level of indentation and <just cause we can>", however. Kind of erring
on the side of +0 with Sara here.

On Sat, Dec 2, 2017 at 1:13 PM, Andreas Hennings <[email protected]>
wrote:

> So to summarize, this feature would save us brackets and one indentation
> level.
> Saving an indentation level can be quite useful to reduce a git commit
> footprint.
>
> On the other hand, it might confuse some people. It would blur the concept
> of a function body scope, and thus the mental model of how the language
> works.
> E.g. are local variables and parameters shared between the try {} and the
> catch {} block?
>
> It would also mean that userland PHP parsers and IDEs need to support the
> syntax.
>
> So personally I am undecided or skeptical. It does feel alien to me.
> Usually if I want to avoid the extra indentation, I will put the try {}
> part into a separate function.
> But this is just me.
>
>
> On 2 December 2017 at 14:27, Alexei Gerasimov <[email protected]>
> wrote:
>
> > > > I did (using this e-mail address), and got the message: "The user has
> > been created and the password was sent by email." However, I did not
> > receive any e-mail with the password.
> > >
> > > Sorry for the trouble, seems like nobody tested the sign up process
> > after the server migration yet. It seems like the e-mail sender
> > configuration is missing.
> > >
> > >
> > > Unfortunately, I don't know the right config, so I can't simply fix it,
> > but I've sent a message to Hannes, he probably knows.
> >
> > Any update? The reset password feature of wiki.php.net doesn't work
> > either, I receive no e-mail.
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
Alexei Gerasimov
Re: [PHP-DEV] C++-like function-try-block proposal
December 02, 2017 09:10PM
> It would blur the concept of a function body scope, and thus the mental model of how the language works.
> E.g. are local variables and parameters shared between the try {} and the catch {} block?

PHP documentation stresses it that the scope is always the entire function, i.e. it isn't like C/C++ where each compound statement makes up a new scope. The construct may indeed look alien to those who haven't had experience with C++, however, I believe this is a matter of a single clarification statement about scope, plus a tip like "just think of it as if wrapping curly braces are there around it, implicitly" (which is in fact the case), and soon you'll find yourself comfortable with it.

> It would also mean that userland PHP parsers and IDEs need to support the syntax.

True. However, this is true for pretty much any language change, and PHP has seen quite a few lately (typehints, anonymous classes.) I don't think we should be worried about changes _other_ projects would have to make to cope up with language evolution lest it becomes an (unnecessary and surrogate) obstacle. My vision is that if some company/group produces an IDE, it is but natural to expect them to be ready to adapt to language changes.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sherif Ramadan
Re: [PHP-DEV] C++-like function-try-block proposal
December 02, 2017 09:20PM
On Sat, Dec 2, 2017 at 3:08 PM, Alexei Gerasimov <[email protected]>
wrote:

>
> I don't think we should be worried about changes _other_ projects would
> have to make to cope up with language evolution lest it becomes an
> (unnecessary and surrogate) obstacle. My vision is that if some
> company/group produces an IDE, it is but natural to expect them to be ready
> to adapt to language changes.
>
>
I agree, because our parser is open source like everything else in PHP.
IDEs can likely just generate one from ours with minimal effort. However,
it isn't the IDEs I'm worried about. It's the end-users of the language
that will struggle the most. Let's not forget PHP 5 has been around for
over a decade before PHP 7 even showed up. There are still a lot of old
code basis that've been running in PHP 5 for years. And for large code
basis with deep rooted legacy, it is no short order to make the leap.
Change for the sake of change won't help that.
Alexei Gerasimov
Re: [PHP-DEV] C++-like function-try-block proposal
December 02, 2017 09:50PM
> I'd like to see an RFC that demonstrates a stronger use case than "avoiding
> one level of indentation and <just cause we can>", however.

Sure, as soon as I am able to register and write on wiki.php.net.

> Let's not forget PHP 5 has been around for over a decade before PHP 7 even
> showed up. There are still a lot of old code basis that've been running in
> PHP 5 for years. And for large code basis with deep rooted legacy, it is no
> short order to make the leap. Change for the sake of change won't help that.

I lost it here. I'm proposing a backward-compatible change, so why speak of
legacy and existing codebases?

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sherif Ramadan
Re: [PHP-DEV] C++-like function-try-block proposal
December 03, 2017 02:50AM
On Sat, Dec 2, 2017 at 3:44 PM, Alexei Gerasimov <[email protected]>
wrote:

>
>
> > Let's not forget PHP 5 has been around for over a decade before PHP 7
> even
> > showed up. There are still a lot of old code basis that've been running
> in
> > PHP 5 for years. And for large code basis with deep rooted legacy, it is
> no
> > short order to make the leap. Change for the sake of change won't help
> that.
>
> I lost it here. I'm proposing a backward-compatible change, so why speak of
> legacy and existing codebases?
>


There's no argument that the change is not forward-compatible. I'm merely
pointing out the burden of adding new language features. This is not to
deter from adding them, but to caution against excess. There is a cost to
learning a new language feature, updating its documentation, considering
its trade offs in software design, etc... Part of this cost is shared
between those who maintain PHP and those who use PHP.

My statements here are intended to be cautionary, not dissuasive.
Niklas Keller
Re: [PHP-DEV] C++-like function-try-block proposal
December 03, 2017 12:20PM
>
> > > I did (using this e-mail address), and got the message: "The user has
> been created and the password was sent by email." However, I did not
> receive any e-mail with the password.
> >
> > Sorry for the trouble, seems like nobody tested the sign up process
> after the server migration yet. It seems like the e-mail sender
> configuration is missing.
> >
> >
> > Unfortunately, I don't know the right config, so I can't simply fix it,
> but I've sent a message to Hannes, he probably knows.
>
> Any update? The reset password feature of wiki.php.net doesn't work
> either, I receive no e-mail.
>

No, sorry. Neither Hannes nor anybody from [email protected] has responded yet.

Regards, Niklas
Christoph M. Becker
Re: [PHP-DEV] C++-like function-try-block proposal
December 03, 2017 01:40PM
On 03.12.2017 at 12:14, Niklas Keller wrote:

> No, sorry. Neither Hannes nor anybody from [email protected] has responded yet.
As a workaround it should be sufficient to request a php.net account via
http://php.net/git-php.php (use the same username and email address);
after somebody has approved the account, access to the Wiki should be
granted.

--
Christoph M. Becker

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Niklas Keller
Re: [PHP-DEV] C++-like function-try-block proposal
December 05, 2017 04:10PM
>
> > No, sorry. Neither Hannes nor anybody from [email protected] has responded yet.
> As a workaround it should be sufficient to request a php.net account via
> http://php.net/git-php.php (use the same username and email address);
> after somebody has approved the account, access to the Wiki should be
> granted.
>

Sorry for the delay, a working configuration is now in place.

I hope you're able to login now after using the password reset feature.

Regards, Niklas
Sorry, only registered users may post in this forum.

Click here to login