> On May 26, 2021, at 7:44 PM, Hendra Gunawan <the.liquid.me...@gmail.com> 
> wrote:
> 
> Hello.
> 
>> 
>> Yes, but Nikita wrote this note about technical limitations at the bottom of 
>> the repo README:
>> 
>> Due to technical limitations, it is not possible to create mutable APIs for
>> primitive types. Modifying $self within the methods is not possible (or
>> rather, will have no effect, as you'd just be changing a copy).
>> 
> 
> If it is solved, this is a great accomplishment for PHP. But I think
> scalar object is not going anywhere in the near future. If you are not
> convinced, please take a look
> https://github.com/nikic/scalar_objects/issues/20#issuecomment-569520181.

Nikita's comment actually causes me more questions, not fewer.

Nikita says "We need to know that $a[$b][$c is an array in order to determine 
that the call should be performed by-reference. However, we already need to 
convert $a, $a[$b] and $a[$b][$c] into references before we know about that."  

How then are we able to do the following?:

$a[$b][$c][] = 1;

How also can we do this:

byref($a[$b][$c]);
function byref(&$x) {
    $x[]= 2;
}

See https://3v4l.org/aPvTD <https://3v4l.org/aPvTD>

I assume that in both my examples $a[$b][$c] would be considered an "lvalue"[1] 
and can be a target of assignment triggered by either the assignment operator 
or calling the function and passing to a by-ref parameter.  

[1] 
https://en.wikipedia.org/wiki/Value_(computer_science)#Assignment:_l-values_and_r-values

So is there a reason that -> on an array could not trigger the same?  Is Nikita 
saying that the performance of those calls performed by-reference would not 
matter because they are always being assigned, at least in the former case, but 
to do so with array expressions would be problematic? (Ignoring there is no 
code in the wild that currently uses the -> operator, or does that matter?)

I ask honestly to understand, and not as a rhetorical question.

Additionally, if the case of updating an array variable is not a problem but 
updating an array expression is a problem then why not just limit the -> 
operator to only work on expressions for immutable methods and require 
variables for mutable methods?  I would think should be easy enough to throw an 
error for those specific "methods" that would be mutable, such as shift() and 
unshift() if $a[$b][$c]->shift('foo') were called?

Or maybe just completely limit using the -> operator on array variables. Don't 
work on any array expressions for consistency. There is already precedence in 
PHP for operators that work on variables and not on expressions:  ++, --, and &.

IF we can get a thumbs up from Nikita that one of these would actually be 
possible then I think the next step should be to write up a list of proposed 
array methods that would be implemented to support the -> operator with arrays 
and put them in an RFC, and to flesh out any edge cases.

-Mike


Reply via email to