Hi all,
Perhaps chiming into the conversation a bit too late, but I would personally 
like to say that I *really* do not like the idea of attribute-like, unvalidated 
generics syntax.

As a maintainer of Psalm and an active user of it both personally and at work, 
I absolutely *adore* the typesafety and ergonomics generics can provide, but 
all benefits are nullified if generics are not not actually typechecked.

Generics can be typechecked by:
- The language itself, which *requires* all types to be correct
- Oe by a static analysis tool which is specifically added to a given project 
for the purpose of typechecking generics: in this case, we can consider to be 
using a superset of PHP which similarly *requires* the static analysis to pass 
in order to "compile" the source code (I.e. if the Psalm pipelines are red, 
deployment is blocked).

Generics whose syntax is fully provided by the language, but then can only be 
*optionally* typechecked by installing third party unofficial tools are not, in 
my opinion, an improvement over the status quo.

Now, after reading this thread I was actually inspired to implement runtime 
typechecked generics in PHP itself, and 
https://github.com/nicelocal/php-src/tree/generics is the single-evening result 
of that: it features generics support in the parser, and some initial, 
partially committed runtime validation.

However, I paused the effort for the reasons listed in 
https://github.com/php/php-src/pull/8752: runtime generics typechecks are 
(needlessly) expensive.

The much better approach, one that I intend to maybe give a shot at this 
Christmas, is to add static analysis functionality to PHP itself (i.e. turn it 
into a truly statically typed language).
I have a hunch it may be easy enough to do by hooking into the type inference 
functionality provided by opcache, and throw compile-time exceptions instead of 
silently inserting runtime typechecks.

This functionality may be initially enabled through a declare(static_types=1); 
statement, and might actually also help find bugs in the type inference 
functionality which causes JIT bugs.

Personally I'm super excited for the possibility of introducing static 
typechecking in PHP itself, partially because it also allows to easily and 
cheaply implement generics.

Regards,
Daniil Gentili

Reply via email to