On Tue, May 25, 2021 at 10:27 PM Dusk <d...@woofle.net> wrote:

> On May 25, 2021, at 09:23, Kamil Tekiela <tekiela...@gmail.com> wrote:
> > I'd like to start a discussion on the following RFC
> > https://wiki.php.net/rfc/autovivification_false
> > Particularly, I am looking for opinions on whether this behaviour should
> be
> > left alone, should be disabled on false, or should be disabled on null
> and
> > false, and left only for undefined variables.
>
> It seems to me there are two different sorts of autovivification which can
> happen. (They may actually be the same thing under the hood, but they feel
> different to me.)
>
> One is autovivification within an array, e.g.
>
>     $x = ["b" => null, "c" => false];
>     $x["a"][] = 1; // from unset
>     $x["b"][] = 2; // from null
>     $x["c"][] = 3; // from false
>
> The other is autovivification on values outside arrays, e.g.
>
>     $b = null; $c = false; $d = new stdClass();
>     $a[] = 1; // from unset
>     $b[] = 2; // from null
>     $c[] = 3; // from false
>     $obj->a[] = 4; // from unset on an object property, same idea
>
> (The same behavior occurs in each case if an explicit key is used instead
> of [].)
>
> From my perspective, the latter is much more concerning than the former.
> Adding dimensions to an existing array feels like less of a
> type-correctness violation than calling an entire array into existence
> where a non-array value (or no value at all) existed before.
>

I agree to that last point. I think the cases so far where I have relied on
autovivification were all like:

```
function f(iterable $xs) {
    $map = []; // initialization
    foreach ($xs as $x) {
        // $map[foo($x)] ??= []; not needed
        // $map[foo($x)][bar($x)] ??= []; not needed
        $map[foo($x)][bar($x)][] = qux($x); // autovivification
    }

    // Then e.g.:
    foreach ($map as $foo => $submap) {
        foreach ($submap as $bar => $quxes) {
            /* ... */
        }
    }
}
```

All other cases I can remember were arguably bugs (missing initialization).

That said, deprecating it on false would already be a +1.

Regards,

-- 
Guilliam Xavier

Reply via email to