Welcome! Log In Create A New Profile

Advanced

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

Posted by reeze 
Hi internals!

I'd like to open a discussion on the RFC to allow set default values for
list() assignment: https://wiki.php.net/rfc/list_default_value.

What is your idea?

Thanks.

--
Reeze Xia
http://reeze.cn
Hi Reeze,

On 9 November 2015 at 13:35, reeze <reeze@php.net> wrote:
> Hi internals!
>
> I'd like to open a discussion on the RFC to allow set default values for
> list() assignment: https://wiki.php.net/rfc/list_default_value.
>
> What is your idea?

I find the list construct to be quite magic already. Isn't it possible
to get the affect that you want without adding anything to the core by
doing this:

$defaults = ['default', 'default'];
$input = [1];

list($a, $b) = array_replace($defaults, $input);

I'd find that way easier to understand and explain to junior devs,
compared to having more functionality added to the 'list' magicness.

cheers
Dan

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Hey Dan,

On 9 November 2015 at 23:24, Dan Ackroyd <danack@basereality.com> wrote:

> Hi Reeze,
>
> On 9 November 2015 at 13:35, reeze <reeze@php.net> wrote:
> > Hi internals!
> >
> > I'd like to open a discussion on the RFC to allow set default values for
> > list() assignment: https://wiki.php.net/rfc/list_default_value.
> >
> > What is your idea?
>
> I find the list construct to be quite magic already. Isn't it possible

to get the affect that you want without adding anything to the core by
> doing this:
>
> $defaults = ['default', 'default'];
> $input = [1];
>
> list($a, $b) = array_replace($defaults, $input);
>
> I'd find that way easier to understand and explain to junior devs,
>

I saw the contrast way, it is more complex ;-). This seems like a trick.
Many feature could also be remove,
such as I refered in RFC: Null coalesce "??" and Ternary Operator "?:"

And the trick is trick. If you want to unpack nested array.

list($a, list(list($b))) = $array

Then you might need a new trick to do that.

You know we have default for function declaration:
function func($a='default') {}; and almost all language have the feature,
it is easier to understand from my perspective.


> compared to having more functionality added to the 'list' magicness.
>
> cheers
> Dan
>



--
Reeze Xia
http://reeze.cn
Bump this to continue discussion of this RFC (
https://wiki.php.net/rfc/list_default_value).

In case some of you didn't follow it before. This RFC propose to allow set
default value in list() assignment:

list($a = 'default value') = $arr;


On 10 November 2015 at 11:14, reeze <reeze@php.net> wrote:

> Hey Dan,
>
> On 9 November 2015 at 23:24, Dan Ackroyd <danack@basereality.com> wrote:
>
>> Hi Reeze,
>>
>> On 9 November 2015 at 13:35, reeze <reeze@php.net> wrote:
>> > Hi internals!
>> >
>> > I'd like to open a discussion on the RFC to allow set default values for
>> > list() assignment: https://wiki.php.net/rfc/list_default_value.
>> >
>> > What is your idea?
>>
>> I find the list construct to be quite magic already. Isn't it possible
>
> to get the affect that you want without adding anything to the core by
>> doing this:
>>
>> $defaults = ['default', 'default'];
>> $input = [1];
>>
>> list($a, $b) = array_replace($defaults, $input);
>>
>> I'd find that way easier to understand and explain to junior devs,
>>
>
> I saw the contrast way, it is more complex ;-). This seems like a trick.
> Many feature could also be remove,
> such as I refered in RFC: Null coalesce "??" and Ternary Operator "?:"
>
> And the trick is trick. If you want to unpack nested array.
>
> list($a, list(list($b))) = $array
>
> Then you might need a new trick to do that.
>
> You know we have default for function declaration:
> function func($a='default') {}; and almost all language have the feature,
> it is easier to understand from my perspective.
>
>
>> compared to having more functionality added to the 'list' magicness.
>>
>> cheers
>> Dan
>>
>
>
>
> --
> Reeze Xia
> http://reeze.cn
>



--
Reeze Xia
http://reeze.cn
On 1/21/2016 8:49 AM, reeze wrote:
> Bump this to continue discussion of this RFC (
> https://wiki.php.net/rfc/list_default_value).
>
> In case some of you didn't follow it before. This RFC propose to allow set
> default value in list() assignment:
>
> list($a = 'default value') = $arr;
>

tl;dr - List is like a contract that the array must honor.

This would seem to me a bad idea as it is already difficult enough getting
people to not think of list() as a function, but as a construct that
imports to named variables elements from an array.

You should also likely only be using list for arrays with known structures,
otherwise how do you know you're defaulting the right variable? Do you
default in front, or in back? Which values are missing from the array that
should be imported to variables and why? Should you not be populating the
array with default values?

For this to even be discussed, I feel like there would need to be a few
concrete examples of how this would be useful. If you're passing an array
to the list construct, you're telling PHP that you know how many values are
in it, and that you want them all to be filled with a value.

For example, in a dynamically generated array, the value that is missing
may be in the middle, but you can't determine where the missing value is,
only that the array is not the appropriate length.

If it's the result of a database query, the query should be returning all
values that will be populated into variables, anything missing is itself
incorrect, and building the list construct in a way to handle the data
being incorrect seems counterproductive. Instead the query should return
all the data that is expected by the construct. List is like a contract
that the array must honor.

Also, what happens when you feed a defaulted list construct bad input that
would currently result in the values being set to NULL? Would they still be
set to NULL, or would they be populated with the default value provided?

Adjusting the example from the documentation:
// list() doesn't work with strings
list($bar = 'default') = "abcde";
var_dump($bar); // NULL

or

var_dump($bar); // string(7) "default"

Sorry if this got a little long. Definitely think there needs to be some
justification behind it, though, and concrete examples of usefulness as
mentioned earlier. And generally speaking, I think treating constructs as
functions in terms of definition should be avoided.

Thanks.
Chris
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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
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
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.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
"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


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
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.
>
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
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
>

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
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
> 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 <bjorn.x.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
>
>
Sorry, only registered users may post in this forum.

Click here to login