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

Reply via email to