Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111877 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 81206 invoked from network); 17 Sep 2020 13:25:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Sep 2020 13:25:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 813691804B1 for ; Thu, 17 Sep 2020 05:34:34 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 17 Sep 2020 05:34:33 -0700 (PDT) Received: by mail-ed1-f41.google.com with SMTP id j2so2298877eds.9 for ; Thu, 17 Sep 2020 05:34:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qtRULEfvWsIFvx5aZQTucCC7CzZ4W+o5lG4bv+cFq1U=; b=L+WWwgrC3JpIT0Ios2oN9sa/P1r4Cxvt32j3zjcVjMikL0hXdkmERtG8gDAjLvlp4m Y/B7K5iTaNrl7/bvJ7ucWx6anMe27/Zlnmql0kZ60Qi0Pxyk9y10MIWI3sZEqFZqtpd4 1bvVtfQ/QlBWsEoJd4GoMGQzcsldl5yqFqFvhxf71o9BfSKpf58tqszwnj8ZogY8cYLX raiLWFMcwNyAXuQIJnGjRZDtP0x1YTCXx3Qc7L141yrevDYCVrDgHQ/2x9RlY4RDwREi W2B5Vnj1kGgc1jSC85G1D8kfXJKUqtoRdhGWovxSxVRYKO+tmVFT/gPwLIj+0yta7FRH /7Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qtRULEfvWsIFvx5aZQTucCC7CzZ4W+o5lG4bv+cFq1U=; b=tsgmfMwQJpVU9mN6NN5gYLrlc9V51hI+dYJ0BTCobPjiwqEs7zxpJzY879YOlXwVMd NgMft2av8x0JEpOlYaAOhmZA4IPsXyE+zhNeEami2MCJcPZNH3GQU/0wQC0/bElELYFw 9O4yluoVcVun6VjuRD0TuKPWPWhMD0TLFhilf8Ztodyv4J3cgorof1bdeAOGjp9Hg1PQ UdmBwbv7DSki6J/EkvSREDuZqoJpKSLmKqlsdo5lOzUzxOKnMDtD5AgZJLnNU2pboV8A RFF/iKKJ7Ie71cmw3OYrrp45BG6hzqTkuQ/jiRJ5QY3CpddLH9AnkAMyLi558U9uiFPg 8rOw== X-Gm-Message-State: AOAM530QrdvJ6TebOHVKIQUtTk1jzGjORd1E6O1QbvhIVx+lMM3PyuMa SbztOCceWBFYqYcUR0VoyBXN65iPaBcb28SYMrcCDUn0wERJcg== X-Google-Smtp-Source: ABdhPJyHhSR5X3a2LTemLGZZE6qCJFyUf323xXTfmfyVuZO3gnvcY6Yw/ksKyN30swqD5vz46YTG+GbncYTUp1mAgr4= X-Received: by 2002:aa7:d88a:: with SMTP id u10mr32997903edq.217.1600346070451; Thu, 17 Sep 2020 05:34:30 -0700 (PDT) MIME-Version: 1.0 References: <09DE673B-71B7-4C8E-A795-900B51F8E476@stitcher.io> In-Reply-To: <09DE673B-71B7-4C8E-A795-900B51F8E476@stitcher.io> Date: Thu, 17 Sep 2020 14:34:19 +0200 Message-ID: To: Brent Roose Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000000c0b0a05af819bb7" Subject: Re: [PHP-DEV] The case for transpiled generics From: george.banyard@gmail.com ("G. P. B.") --0000000000000c0b0a05af819bb7 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, 17 Sep 2020 at 13:33, Brent Roose wrote: > Hello internals > > Today I'd like to hear your thoughts on what might be a controversial > topic, though I think it's worth having this discussion. I want to make t= he > case for adding generic syntax, without actually enforing any additional > type checks at runtime. Please hear me out. > > We've been discussing generics for years now [1][2], all without any > result. Nikita's latest attempt [3] stalled because, from what I gathered > and amongst other things, doing generic type checks at runtime has a > significant impact on performance. > > On the other hand, static analysers have been making their rise for a few > years now. Granted: not the whole community might like this kind of type > strictness, and PHP doesn't force them to; but still projects like PhpSto= rm > acknowledge their significance =E2=80=94 they will add built-in support f= or both > psalm and PHPStan later this year [4]. Rasmus Lerdorf also showed interes= t > in the idea of improving PHP's static analysis capabilities two years ago > [5]. > > That all to say that there's a significant part of the PHP community who'= s > interested in embracing the benefits of static analysis. > > If we look outside of our PHP bubble, we can see the same thing happening > in JavaScript: the core benefit that TypeScript adds is its robust static > analysis. Sure those developers need an extra compilation step to transpi= le > their code to plain old JavaScript, but it seems that they are=E2=80=A6 f= ine with > that? > > I'd like to discuss a similar idea for PHP. If runtime generics aren't > possible because of performance issues, why not explore the other option: > adding generic syntax that is ignored by the interpreter, but can be used > by static analysis tools =E2=80=94 third party of built-into PHP, that's = another > discussion. I realise this thought goes against the "PHP mindset" we've > been programming with for more than 20 years, but we I think we shouldn't > ignore what's happening in the PHP- and wider progamming community: stati= c > analysis is relevant, whether you want to use it or not, and a stricter > type system is prefered by many. > > Now I know there are alternatives we can use today. Static analysers > already support generics, using doc blocks. I'm not trying to argue that > it's impossible to achieve the same results with the toolset we have, but > rather that there's room for improvement from the developer's point of > view. History has shown that such convenience additions to PHP have been = a > difficult pill to swallow for some, but on the other hand those kind of > changes _have_ been happening more and more often anyway: property > promotion, short closures, named arguments, attributes, yes even types > themselves: you can write the same working PHP program without any of tho= se > features, and yet they have been proven so useful and wanted over the las= t > years. > > As a sidenote: the idea of transpiling is already present in PHP. Looking > at constructor property promotion: a purely syntactical feature, which is > transformed to simpler PHP code at runtime. Nikita called this principle > "desugaring" in the constructor property promotion RFC [6]. > > So here's my case for transpiled generics summarized: > > - There's no significant runtime performance impact > - The PHP community is already embracing static analysis > - Transpiling has been proved to be a viable workflow, thanks to TypeScri= pt > - As with all things-PHP: it's opt-in. You don't have to use the syntax i= f > you don't want to and you won't experience any downsides > > So with all that being said, I'm looking forward to hearing your thoughts= . > > Kind regards > Brent > > [1] https://wiki.php.net/rfc/generics > [2] https://wiki.php.net/rfc/generic-arrays < > https://wiki.php.net/rfc/generic-arrays> > [3] https://github.com/PHPGenerics/php-generics-rfc/issues/45 < > https://github.com/PHPGenerics/php-generics-rfc/issues/45> > [4] > https://blog.jetbrains.com/phpstorm/2020/07/phpstan-and-psalm-support-com= ing-to-phpstorm/ > < > https://blog.jetbrains.com/phpstorm/2020/07/phpstan-and-psalm-support-com= ing-to-phpstorm/> > > [5] https://externals.io/message/101477#101592 < > https://externals.io/message/101477#101592> > [6] https://wiki.php.net/rfc/constructor_promotion#desugaring < > https://wiki.php.net/rfc/constructor_promotion#desugaring> 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_projec= t and voil=C3=A0 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. As such I see no benefit in supporting generics at the language level if they don't have any runtime checks. Best regards George P. Banyard [1] https://www.reddit.com/r/PHP/comments/i9h1v8/considering_php/g1h9umy?contex= t=3D3 --0000000000000c0b0a05af819bb7--