Page MenuHomePhabricator

Make it possible to add array elements dynamically
Open, LowestPublic

Description

This snippet doesn't work:

arr := [];
arr[0] := 1; // Error: outofbound

because the parser checks whether arr[0] exists before the assignment. Instead, you have to specify the array size when you initialize the array, so this will work:

arr := [ 'foo' ];
arr[0] := 1;

I think we should either:

  • Not check whether the array element exists in assignments (this should be trivial in the new parser, which distinguishes INDEX_ASSIGNMENT and ARRAY_INDEX, but it might be harder in the old one)
  • Keep checking, but create an empty element on the fly if it doesn't already exist

Unsure what the best approach would be (they might well be equivalent, given how simple the filter language is).

Event Timeline

Note, this would also make it possible to create gaps. If any code relies on the absence of gaps, it should be fixed as well.

What is the use case for this? What you are trying to do is to use "array" for both hash table and array, just like what PHP does, and I don't think that's a good idea: it makes reasoning about code much more difficult, and prone to mistakes for people who come from other languages that don't have this feature. You can also see that HHVM tries to go in the opposite direction: they deprecated PHP array and created vec and dict (see https://docs.hhvm.com/hack/built-in-types/arrays).

What is the use case for this? What you are trying to do is to use "array" for both hash table and array, just like what PHP does, and I don't think that's a good idea: it makes reasoning about code much more difficult, and prone to mistakes for people who come from other languages that don't have this feature. You can also see that HHVM tries to go in the opposite direction: they deprecated PHP array and created vec and dict (see https://docs.hhvm.com/hack/built-in-types/arrays).

Hah, that's interesting. I have to say that I don't have precise use cases, I just thought that implementing this feature would have a very low cost. If this isn't true, let's take a step back and close this task as declined.

Daimona triaged this task as Lowest priority.Sep 16 2020, 1:56 PM
Daimona removed a project: User-Daimona.

In the lack of a valid use case, and given the difficulties mentioned above.