Skip to content

Zend: add type polymorphism to add_{next_}index_TYPE() functions - #24628

Draft
Girgias wants to merge 2 commits into
php:masterfrom
Girgias:2026-10-generic-add_index
Draft

Girgias wants to merge 2 commits into
php:masterfrom
Girgias:2026-10-generic-add_index

Conversation

@Girgias

@Girgias Girgias commented Oct 24, 2026

Copy link
Copy Markdown
Member

As it is ridiculous that we only have convenient APIs to add types for HashTables wrapped in zvals but not HashTables directly.
But now that we require C11 it means we can rely on _Generic to exist.
However C's _Generic is an infuriating mess to use as every single branch must be semantically valid even if it is discarded.
Not at all confusing behaviour with terrible compiler errors, it took me a while to understand why the "obvious" ways of writing the generics did not work.
Until I managed to stumble upon [1] (thank Kagi as Google/DDG have been useless) that explains the observed behaviour.
It is possible for us to have parametric genericity because we are branching on pointers.
The trick is to create dummy functions that take void* arguments and return a HashTable* rather than trying to use a macro.

The C++ side is simple function overloading.

[1] https://www.chiark.greenend.org.uk/~sgtatham/quasiblog/c11-generic/#bigbug

As it is ridiculous that we only have convenient APIs to add types for HashTables wrapped in zvals but not HashTables directly.
But now that we require C11 it means we can rely on _Generic to exist.
However C's _Generic is an infuriating mess to use as every single branch must be semantically valid even if it is discarded.
Not at all confusing behaviour with terrible compiler errors, it took me a while to understand why the "obvious" ways of writing the generics did not work.
Until I managed to stumble upon [1] (thank Kagi as Google/DDG have been useless) that explains the observed behaviour.
It is possible for us to have parametric genericity because we are branching on pointers.
The trick is to create dummy functions that take void* arguments and return a HashTable* rather than trying to use a macro.

The C++ side is simple function overloading.

[1] https://www.chiark.greenend.org.uk/~sgtatham/quasiblog/c11-generic/#bigbug
Comment thread Zend/zend_API_generic.h
* [1] https://www.chiark.greenend.org.uk/~sgtatham/quasiblog/c11-generic/#bigbug
*/

static inline HashTable *htid(void *ptr) { return ptr; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be best for symbols clarity for these little helpers to have a zend_ prefix wdyt ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was trying to get it to work first :) I also need to update the header define too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants