On 11/03/2024 12:52, Gina P. Banyard wrote:
I would like to get some initial feedback on an RFC I've been working on for
the last 5–6 months.
The RFC attempts to explain, and most importantly, improve the semantics around
$container[$offset] as PHP is currently widely inconsistent.
[...]
RFC:
https://github.com/Girgias/php-rfcs/blob/master/container-offset-behaviour.md
Hi Gina,
I've just read through this thoroughly, and am simultaneously impressed
with your investigation, and amazed at how many inconsistencies you found.
I think the proposed granular interfaces absolutely make sense, given
the different uses people have for such offsets. My only hesitation is
that if you want "everything", things become quite verbose:
class Foo implements DimensionFetchable, DimensionWritable,
FetchAppendable, DimensionUnsettable { ... }
function
bar(DimensionFetchable&DimensionWritable&FetchAppendable&DimensionUnsettable
$container) { ... }
Unfortunately, I can't think of an easy solution to this without some
form of type aliases.
As an experiment, I tried writing a variation of Python's "defaultdict"
[1] using all the new hooks (without actually testing it against any
implementation). Here's what I came up with:
https://gist.github.com/IMSoP/fbd60c5379ccefcab6c5af25eacc259b
Most of it is straight-forward, but a couple of things stood out:
* Separating offsetFetch from offsetGet is really useful, because we can
avoid "auto-vivifying" a key that's only been read, never updated. In
other words, isset($foo['a']) can remain false after running
var_dump($foo['a']), but $foo['a']++ should still work.
* The fetchAppend hook is quite confusing to implement, because it's
used in a few subtly different scenarios. For instance, if it's actually
$container[][$offset] = $value there is an implicit requirement that
fetchAppend should return array|DimensionWritable, but presumably that
has to be enforced after fetchAppend has returned. I'm not sure if
there's anything that can be improved here; it probably just needs some
examples in the user manual.
[1]
https://docs.python.org/3/library/collections.html#collections.defaultdict
Over all, I think this is a really great proposal, and hope it proceeds
smoothly.
Regards,
--
Rowan Tommins
[IMSoP]