Re: The case for transpiled generics
| From: | Brent Roose | Date: | Thu, 17 Sep 2020 17:22:09 +0000 |
| Subject: | Re: The case for transpiled generics | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111888@lists.php.net to get a copy of this message | ||
Hi George
Thanks for the reply!
> On 17 Sep 2020, at 14:34, G. P. B. <george.banyard@gmail.com> wrote:
>
> Hello Brent,
>
> I'm going to make the argument I've already done on Reddit once [1], IMHO
> TypeScript is just a nicer pipeline for a preprocessor and a static
> analyser and not a language per say.
>
> Let me explain with a simpler example than generics, adding immutable
> objects to PHP without native language support using a preprocessor and
> Psalm as the back-end for static-analysis. It would be rather trivial to do
> a search-and-replace of the word immutable and replace it with /**
> @psalm-immutable */ at the (currently non existent) preprocessor level then
> run Psalm and have it shout at you if "you are doing it wrong"TM.
> Now bundle the two tools into a nice CLI command run TypePHP root_of_project
> and voilà support for immutable by just writing
>
> immutable class Foo {}
>
> And I'm fairly certain that one can achieve support for generic templates
> in the same way, probably harder than adding support for immutable objects.
> It is also possible to have various back-ends for the static analysers
> which would change the doc-annotation to the one understood by the analyser
> asked.
>
> Most importantly this can be done purely in PHP and distributed as a
> composer package.
> Therefore needing no support from the language itself, thus this can even
> work in PHP 7.
Preprocessing PHP has been done [1], and the reality is that it didn't work. Not because
it's technically impossible, but because there's no tooling around it: no IDE integration
and no static analysis. The pre project actually tried developing a plugin for PhpStorm, which
failed due to plugin API limitations. Tooling and IDEs tend to follow the language spec, because
there's no way around it. That power lies within the PHP project, and no one else in the
community has the same amount of influence.
>
> As such I see no benefit in supporting generics at the language level if
> they don't have any runtime checks.
Like I said in my mail: it's not about being able to achieve a specific kind of functionality
— static analysers already support generics — it's about giving developers an easy,
integrated way of writing them, with as less overhead as possible. I think most new features added
to PHP these days are a testament to that mindset — I've already mentioned them in my
previous post.
>
> Best regards
>
> George P. Banyard
>
> [1]
>
> https://www.reddit.com/r/PHP/comments/i9h1v8/considering_php/g1h9umy?context=3
King regards
Brent
[1] https://preprocess.io/#/