Re: State of Generics and Collections
| From: | Rob Landers | Date: | Mon, 19 Aug 2024 17:49:48 +0000 |
| Subject: | Re: State of Generics and Collections | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125050@lists.php.net to get a copy of this message | ||
On Mon, Aug 19, 2024, at 19:08, Derick Rethans wrote:
> Hi!
>
> Arnaud, Larry, and I have been working on an article describing the
> state of generics and collections, and related "experiments".
>
> You can find this article on the PHP Foundation's Blog:
>
> https://thephp.foundation/blog/2024/08/19/state-of-generics-and-collections/
>
> cheers,
> Derick
>
Nice! It is awesome to see some movement here. Just one thing:
> Invariance would make arrays very difficult to adopt, as a library can not start type hinting
> generic arrays without breaking user code, and users can not pass generic arrays to libraries until
> they start using generic arrays type declarations.
This seems like a strawman argument, to a degree. In other words, it seems like you could combine
static arrays and fluid arrays to accomplish what you are seeking to do. In other words, use static
arrays but allow casting to treat it as "fluid."
In other words, simply cast to get your example to compile:
function f(array<int> $a) {}
function g(array $a) {}
$a = (array<int>) [1]; // array unless cast
f($a); // ok
g((array)$a); // ok
And the other way:
function f(array<int> $a) {}
function g(array $a) {}
$a = [1];
f((array<int>)$a); // ok, type check done during cast
g($a); // ok
— Rob