Re: [PHP-DEV] [RFC][DISCUSSION] Allow default value in list() syntax

  100172
August 10, 2017 21:21 andreas@dqxtech.net (Andreas Hennings)
Hello list,

I found this RFC, describing a feature I wanted for a long time:
https://wiki.php.net/rfc/list_default_value
https://marc.info/?l=php-internals&m=144707619509724

(I don't know how to correctly reply to old emails that are not in my
inbox, sorry for that. I don't like mailing lists.)

I regularly want this kind of feature when unpacking a string with explode():

list($prefix, $suffix = NULL) = explode(':', $string);
if (NULL !== $suffix) {
  ...
}

This is for situations where a string is coming from somewhere, we
hope it has the format "aaa:bbb", but we cannot be sure.

Sometimes I use a workaround like this:

list($prefix, $suffix) = explode(':', $string . ':');
if ('' !== $suffix) {
  ...
}
Which works, but means a new string will be allocated in memory.

I find the list() with default values to be a decent and
understandable language feature.

-- Andreas
  100173
August 10, 2017 21:22 andreas@dqxtech.net (Andreas Hennings)
I don't have a strong opinion about the nested list() construct which
is also part of the RFC.
I never had a situation where I wanted this.

On Thu, Aug 10, 2017 at 11:21 PM, Andreas Hennings <andreas@dqxtech.net> wrote:
> Hello list, > > I found this RFC, describing a feature I wanted for a long time: > https://wiki.php.net/rfc/list_default_value > https://marc.info/?l=php-internals&m=144707619509724 > > (I don't know how to correctly reply to old emails that are not in my > inbox, sorry for that. I don't like mailing lists.) > > I regularly want this kind of feature when unpacking a string with explode(): > > list($prefix, $suffix = NULL) = explode(':', $string); > if (NULL !== $suffix) { > ... > } > > This is for situations where a string is coming from somewhere, we > hope it has the format "aaa:bbb", but we cannot be sure. > > Sometimes I use a workaround like this: > > list($prefix, $suffix) = explode(':', $string . ':'); > if ('' !== $suffix) { > ... > } > Which works, but means a new string will be allocated in memory. > > I find the list() with default values to be a decent and > understandable language feature. > > -- Andreas
  100174
August 10, 2017 21:33 pollita@php.net (Sara Golemon)
On Thu, Aug 10, 2017 at 5:21 PM, Andreas Hennings <andreas@dqxtech.net> wrote:
> I found this RFC, describing a feature I wanted for a long time: > https://wiki.php.net/rfc/list_default_value > https://marc.info/?l=php-internals&m=144707619509724 > > I regularly want this kind of feature when unpacking a string with explode(): > > list($prefix, $suffix = NULL) = explode(':', $string); > if (NULL !== $suffix) { > ... > } > Agreed. This comes up for me every time I want to parse a text file
(including about 30 minutes ago, as it happens). +1 -Sara
  100175
August 10, 2017 22:01 devnuhl@gmail.com (Devnuhl Unnamed)
Would isset($suffix) not suffice here? There are several things I've
thought of that would be other alternatives to changing this language
construct, but as I don't have my laptop with me, some of the testing to
confirm behavior will have to wait.

Other concerns fall around list() already being a difficult thing for
people to understand, let alone use properly. Making it look even more like
a function definition would likely exacerbate this.

-Chris


On Aug 10, 2017 16:33, "Sara Golemon" <pollita@php.net> wrote:

On Thu, Aug 10, 2017 at 5:21 PM, Andreas Hennings <andreas@dqxtech.net>
wrote:
> I found this RFC, describing a feature I wanted for a long time: > https://wiki.php.net/rfc/list_default_value > https://marc.info/?l=php-internals&m=144707619509724 > > I regularly want this kind of feature when unpacking a string with explode():
> > list($prefix, $suffix = NULL) = explode(':', $string); > if (NULL !== $suffix) { > ... > } > Agreed. This comes up for me every time I want to parse a text file
(including about 30 minutes ago, as it happens). +1 -Sara
  100176
August 10, 2017 22:40 andreas@dqxtech.net (Andreas Hennings)
On Fri, Aug 11, 2017 at 12:01 AM, Devnuhl Unnamed <devnuhl@gmail.com> wrote:
> Would isset($suffix) not suffice here?
You mean like so? list($prefix, $suffix) = explode(':', 'string_without_suffix'); if (!isset($suffix)) { .. } The isset() is too late here, because the list() will already cause an error.
> Other concerns fall around list() already being a difficult thing for people to understand
I fail to understand what is difficult about it.. And "destructuring" is a common concept in programming languages. https://www.startpage.com/do/search?query=destructuring&cat=web&pl=chrome&language=english https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment The following already works in Javascript: [a, b, c, d = 'else'] = ['aa', 'bb', 'cc']; I just tried it in my Chromium console.
  100178
August 11, 2017 07:51 TonyMarston@hotmail.com ("Tony Marston")
"Andreas Hennings"  wrote in message 
news:CAH0Uv3HQK5wjcd_-9GynMw34H78ZTv09q9bc=yZ10JBbeT=VOA@mail.gmail.com...
> >On Fri, Aug 11, 2017 at 12:01 AM, Devnuhl Unnamed <devnuhl@gmail.com> >wrote: >> Would isset($suffix) not suffice here? > >You mean like so? > >list($prefix, $suffix) = explode(':', 'string_without_suffix'); >if (!isset($suffix)) { > .. >} > >The isset() is too late here, because the list() will already cause an >error. > > >> Other concerns fall around list() already being a difficult thing for >> people to understand
I agree.
>I fail to understand what is difficult about it.
The point is that other people have difficulty understanding what is supposed to be a simple feature which is now being expanded to include more options which change its behaviour.
>And "destructuring" is a common concept in programming languages. >https://www.startpage.com/do/search >?query=destructuring&cat=web&pl=chrome&language=english >https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment > >The following already works in Javascript: >[a, b, c, d = 'else'] = ['aa', 'bb', 'cc']; >I just tried it in my Chromium console.
That fact that something like this exists in another language is no reason to add it to PHP. -- Tony Marston
  100185
August 11, 2017 14:18 devnuhl@gmail.com (Devnuhl Unnamed)
When you understand something, it often can be hard to understand why
others wouldn't. I don't see why people struggle with it either, but I do
see it happen quite a lot.

I would still think that if you are going to deconstruct something in a
structured way, you should validate your data is correct beforehand. And
that would be the more ideal solution here. But if replies to a genuine
concern over things I've witnessed is going to be met with a dismissive
attitude, I'm just going to bow out entirely.

Chris

On Aug 10, 2017 5:40 PM, "Andreas Hennings" <andreas@dqxtech.net> wrote:

> On Fri, Aug 11, 2017 at 12:01 AM, Devnuhl Unnamed <devnuhl@gmail.com> > wrote: > > Would isset($suffix) not suffice here? > > You mean like so? > > list($prefix, $suffix) = explode(':', 'string_without_suffix'); > if (!isset($suffix)) { > .. > } > > The isset() is too late here, because the list() will already cause an > error. > > > > Other concerns fall around list() already being a difficult thing for > people to understand > > I fail to understand what is difficult about it.. > And "destructuring" is a common concept in programming languages. > https://www.startpage.com/do/search?query=destructuring& > cat=web&pl=chrome&language=english > https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/ > Destructuring_assignment > > The following already works in Javascript: > [a, b, c, d = 'else'] = ['aa', 'bb', 'cc']; > I just tried it in my Chromium console. >
  100188
August 11, 2017 17:46 gmblar@gmail.com (Andreas Treichel)
You can merge the result with default values like this:

[$foo, $bar, $foobar] = explode(':', 'foo:bar') + [23, 42, 1337];
var_dump($foo, $bar, $foobar);
  100190
August 12, 2017 00:37 andreas@dqxtech.net (Andreas Hennings)
This is true, I remember having done it in the past.

I still think it would be nice and feel natural to have the default
values directly built into the list construct.

It would be a bit faster, because it does not have to allocate a new
temporary array.

Whether this difference matters depends on the case.
In my own philosophy, every code that I write to be reusable, could at
some point be used in a situation where it is repeated a lot, and thus
has a performance impact. So for my taste, it does matter.

This said, I understand it not having the greatest priority.




On Fri, Aug 11, 2017 at 7:46 PM, Andreas Treichel <gmblar@gmail.com> wrote:
> You can merge the result with default values like this: > > [$foo, $bar, $foobar] = explode(':', 'foo:bar') + [23, 42, 1337]; > var_dump($foo, $bar, $foobar); > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
  100208
August 14, 2017 09:35 bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=)
Den 2017-08-12 kl. 02:37, skrev Andreas Hennings:

> This is true, I remember having done it in the past. > > I still think it would be nice and feel natural to have the default > values directly built into the list construct. > > It would be a bit faster, because it does not have to allocate a new > temporary array. > > Whether this difference matters depends on the case. > In my own philosophy, every code that I write to be reusable, could at > some point be used in a situation where it is repeated a lot, and thus > has a performance impact. So for my taste, it does matter. > > This said, I understand it not having the greatest priority. > > > > On Fri, Aug 11, 2017 at 7:46 PM, Andreas Treichel <gmblar@gmail.com> wrote: >> You can merge the result with default values like this: >> >> [$foo, $bar, $foobar] = explode(':', 'foo:bar') + [23, 42, 1337]; >> var_dump($foo, $bar, $foobar); >> >> >> >> -- >> PHP Internals - PHP Runtime Development Mailing List >> To unsubscribe, visit: http://www.php.net/unsub.php >> One could argue that:
[$foo, $bar, $foobar = 1337] = explode(':', 'foo:bar'); is more readable. I find it a small but nice addition that doesn't clutter the language. r//Björn
  100210
August 14, 2017 11:34 ocramius@gmail.com (Marco Pivetta)
> that doesn't clutter the language.
It actually does: now we have an expression-alike node that is lazy and only evaluated when there's no default value. That's a lot of added semantics for such an edge case feature. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On Mon, Aug 14, 2017 at 11:35 AM, Björn Larsson larsson@telia..com> wrote:
> Den 2017-08-12 kl. 02:37, skrev Andreas Hennings: > > This is true, I remember having done it in the past. >> >> I still think it would be nice and feel natural to have the default >> values directly built into the list construct. >> >> It would be a bit faster, because it does not have to allocate a new >> temporary array. >> >> Whether this difference matters depends on the case. >> In my own philosophy, every code that I write to be reusable, could at >> some point be used in a situation where it is repeated a lot, and thus >> has a performance impact. So for my taste, it does matter. >> >> This said, I understand it not having the greatest priority. >> >> >> >> On Fri, Aug 11, 2017 at 7:46 PM, Andreas Treichel <gmblar@gmail.com> >> wrote: >> >>> You can merge the result with default values like this: >>> >>> [$foo, $bar, $foobar] = explode(':', 'foo:bar') + [23, 42, 1337]; >>> var_dump($foo, $bar, $foobar); >>> >>> >>> >>> -- >>> PHP Internals - PHP Runtime Development Mailing List >>> To unsubscribe, visit: http://www.php.net/unsub.php >>> >>> One could argue that: > [$foo, $bar, $foobar = 1337] = explode(':', 'foo:bar'); > > is more readable. I find it a small but nice addition > that doesn't clutter the language. > > r//Björn > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >