Re: [Initial Feedback] Typed Arrays
| From: | Richard Miles | Date: | Sun, 30 Jun 2024 23:10:59 +0000 |
| Subject: | Re: [Initial Feedback] Typed Arrays | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-124117@lists.php.net to get a copy of this message | ||
>> If I were to choose between typed-array and collection like Derick showed a little bit
>> I'd choose typed arrays definitely as a first feature to be merged into PHP.
Cheers indeed. I’m still looking for a karma sponsor ;)
> Tuples are nice because they are bounded, they can probably act like arrays …
I’ve been leaning toward this working under the hood like SplObjectStorage and SplFixedArray.
For those who may not be super familiar, I’ve added the PHP documentation links:
https://www.php.net/manual/en/class.splobjectstorage.php
https://www.php.net/manual/en/class.splfixedarray.php
A typed array would/may not be a hash table, but a fixed list of some kind. I’ve had a few
suggestions
and one individual in the
is operator conversation said hash tables may be appropriate
anyway.
Time will tell on this front and its implementation is worth the open discussion. What should be
absolute
is that arrays with a ‘fixed’ type maynot assign members whoes types are not strictly defined.
Thus, typed
arrays are stictly bound.
SplFixedArray acted like numeric Seq<> whereas SplObjectStorge adds functionionallity to our
normal
arrays and acts our Dict<>.
I think a big issue this proposal solves is how to syntactically approach saying a member is present
or null
or if member is present it should only be a string. The initial proposed syntax in the early
feedback of the
pattern matching rfc poses:
> So yes, [?'foo' => string] and
> ['foo' => ?string] are indeed
> different.
A lot of discussion around this, but I think with interfaces this becomes trivail. Note my example
code sovles
this using operators we already have: &, |, &|
interface iArrayA ['a' => string ]
interface iArrayB implements iArrayA ['b' => string, 'c' => ?string ]
// reads the same as a typecast
$array = (iArrayA &| iArrayB) [
‘a’ => ‘hello'
];
> … (possibly even cast to them and vice-versa).
The way I implemented declaring types is actually just a typecast, so perhaps that should change or
we just extend the current array implementation to hold another metadata field.?
Best,
Richard Miles