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

php.internals

Reeze Xia

10 years ago
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

Dan Ackroyd

10 years ago
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

Reeze Xia

10 years ago
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

10 years ago
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

randomly there

10 years ago
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