RE: [PHP-DEV] [RFC] [Discussion] Bound-Erased Generic Types
| From: | Luke Diggins | Date: | Sun, 07 Jun 2026 05:53:05 +0000 |
| Subject: | RE: [PHP-DEV] [RFC] [Discussion] Bound-Erased Generic Types | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131160@lists.php.net to get a copy of this message | ||
Hi Seifeddine,
>> But count me out for opt-in enforcement. We either enforce it or we don't. [...] If
>> call-site enforcement can be made cheap enough to include, it's cheap enough to
>> *always*
>> include.
>
> The premise here is that runtime enforcement can be made cheap enough
> in the foreseeable future. That's the universal-reification path, and
> no implementation has yet demonstrated it works under PHP's
> compilation model. Until we have a solution, "always enforce" isn't a
> deferrable engineering choice, it's a constraint we currently can't
> meet. If someone produces a viable design, the path forward is either
> an opt-in mechanism (using the #[ReifiedGenerics] attribute, the reify
> keyword, or the declare directive) or a complete switch to reified
> generics, which would involve a breaking change (BC break). What I
> won't commit to is "we'll just make it cheap enough later" when the
> engineers who have tried haven't been able to.
Even if runtime enforcement can never be made cheap enough, I believe it's
a good thing for type erased generics' syntax to clearly express the
inconsistency with regular runtime checked type hints. Runtime type
checking is a form of safety offered by PHP, and by default safety options
should be opt-out, not opt-in. Even for best practice developers using
static analysis, seeing 'erased' explicitly in the syntax might be just the gentle
reminder we need while debugging a real-world edge case.
The word 'reified' means nothing to most people other than language
designers, and whether PHP's generics are reified or monomorphised is an
implementation detail developers need not know about.
#[TypeErased] has
a much better chance of being understood by developers as type unchecked
when they come across code using it.
In summary:
- If reified generics never happen, and our code is forever left with erased keywords/attributes,
that's still helpful.
- If reified generics do happen, we get the choice to remove our erased keywords/attributes,
that's ideal.
- The only bad scenario is reified generics do happen performantly but we have to opt-in to them
everywhere.
Cheers,
Luke