On Wed, Oct 18, 2023 at 5:26 AM Deleu <deleu...@gmail.com> wrote:
>
> On Tue, Oct 17, 2023 at 3:43 PM Brandon Jackson <brandonja...@gmail.com>
> wrote:
>
> > > There is also a technique to make the return value `[$key => $value]`
> > instead of just a value, but this loses simplicity.
> >
> > Hmm, since the naming array_first and array_last doesn't clarify that
> > it's returning a key or a value. What if it returned both as ?[key,
> > value].
> >
> > That opens quite a few use possibilities:
> > $first = array_first($array);
> > $value = $first[1] ?? throw new Exception();
> >
> > [,$value] = array_first($array) ?? [null, null];
> > [,$value] = array_first($array) ?? throw new Exception();


Hey Marco,

> This function signature can be accomplished by userland once we have
> `array_key_first()` and `array_first()`.

This would always mean you have to keep them right next to each other,
it would be a best practice to do so and to split them up should be a
code smell in any static analysis. There is no way to tell if a Fiber
is involved in any function call in PHP, thus if you split them apart
and call a function, it is possible that your current Fiber is
suspended and another Fiber mutates the variable you are referencing
(this is especially true in Classes, not so much in pure functions).
Since they would always have to be right next to each other, it is
easier to just combine them into a single atomic function call, which
would negate the need for static analysis to be involved or surprises.

> It's much better to keep
> `array_first()` as simple as possible and let everyone build their own
> approach to go about it since we have so many approaches.

There is only one right approach that prevents Fibers from messing up
your day, and it would be considerable boilerplate code that you'd
have to type every time, as well as involve static analysis and watch
for "people who don't know" better in code reviews.

Robert Landers
Software Engineer
Utrecht NL

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to