# [discussion] arraykey type

- Thread: https://externals.io/message/105465
- Original archive: https://news-web.php.net/php.internals/105465
- Started by azjezz on 2019-04-26T23:13:53+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #105465 — azjezz — 2019-04-26T23:13:53+00:00

URL: https://externals.io/message/105465#105465

Hello Internals,

As [Danack](https://github.com/Danack) has mentioned in the [PR comments](https://github.com/php/php-src/pull/4073#issuecomment-487186654), i want to discuss this here before writing an RFC.

What do you think about adding the `arraykey` type to PHP.

see :
- https://docs.hhvm.com/hack/types/arraykey
- https://github.com/php/php-src/pull/4073#issuecomment-487178779
-https://github.com/php/php-src/pull/2603#issuecomment-487122061
- https://twitter.com/azjezz/status/1121182765385433095
- https://www.facebook.com/groups/2204685680/?multi_permalinks=10157789745805681

GitHub pull request : https://github.com/php/php-src/pull/4073

---

### Weak typing
As Nikita [mentioned in the PR](https://github.com/php/php-src/pull/4073#issuecomment-487184224), this is an issue that we are facing even with union types.

assuming we have a function named `get` with the following signature :
```
function get(iterable $container, arraykey $index): mixed;
```
should passing a `float` or `bool` be allowed when `strict_types=0` ?
if so, should `get($arr, true);` act as `get($arr, 1);` or `get($arr, '1');` ?

---

### Naming
The `arraykey` naming comes from Hack, i found it familiar since i am used to Hack, but we have other options :

- `index`
- `key`
- `offset`

---

Best regards,

Saif Eddin Gmati.

---

## #105470 — Dan Ackroyd — 2019-04-27T18:26:53+00:00

URL: https://externals.io/message/105465#105470

>
> What do you think about adding the `arraykey` type to PHP.


I don't think we should be adding anything to PHP core unless:

* there is a strong case for it being needed.
* we're sure it's not going to cause problems in the future.
* it's clearly the correct thing to implement.
* there's good reasons not to implement it in userland.

I'm pretty sure this idea fails all of those tests, except the last one.

Without generics, it seems the need for an general arraykey type is
low, for me at least. For the vast majority of the arrays I use, I
don't mix key types. All of the entries in an array will be either
0-based incrementing integer keys, string based keys.

I think the Union Types RFC https://wiki.php.net/rfc/union_types has a
high chance of being revisited in the future. As Nikic mentioned in
the PR  we would need to make sure that arraykey would be compatible
with it. That is very hard to get right without having the Union Types
RFC already implemented, as the behaviour around casting is going to
be tricky.

Or, more simply, there are some parts of PHP that can be made better
by adding small bits at a time. I don't think the type system is one
of those parts, and it's better to get the big pieces in place first,
so that the smaller bits will fit accordingly with those.

If you wanted to proceed with it, I think you'd need to find a really
strong reason for why it should be added, rather than just it could be
added.

cheers
Dan
