Re: Adding `final class Deque` to PHP
| From: | tyson andre | Date: | Wed, 22 Sep 2021 04:03:11 +0000 |
| Subject: | Re: Adding `final class Deque` to PHP | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-116123@lists.php.net to get a copy of this message | ||
Hi Levi Morrison,
> > "Maximum size" shouldn't be an issue for
> >
Deque specifically, it's a
> > size_t. It isn't an issue for SplFixedArray and ArrayObject.
> > (or for
> > PHP would just encounter a fatal error due to either
>
> You wrote a lot, but unfortunately it was based on a misunderstanding.
> In some languages you can set the maximum allowed number of items a
> specific Deque can hold. For example (pseudo-code):
>
> let deque = new Deque<int>(max_capacity: 3)
> deque.push_back(1)
> deque.push_back(2)
> deque.push_back(3) # okay, it's now full
>
> deque.push_back(4) # !
>
> In this condition, they either error or remove the earliest.
>
> It's okay if the proposed Deque doesn't add this capability, but it's
> the only remaining major functionality which some languages have but
> not the others; it should at least be discussed, I think.
Oh, I hadn't remembered seeing that before. That makes sense.
All of the implementations I'd remembered were unlimited.
(https://cplusplus.com/reference/deque/deque/max_size/ was a system limit, for example)
I can't think of a common use case for that in php.
If there was one, though, I strongly believe it shouldn't be the same class (and shouldn't
be a subclass),
in order to ensure the behavior of push or other operations remains easy to reason about.
This can be done in userland as a userland class.
- (e.g. with a Deque instance property and runtime count() < $this->maxCapacity
checks,
to choose their own desired return value or Throwable subclass (or manual array and circular
buffer in PHP)
Thanks,
Tyson