Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111875 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 74815 invoked from network); 17 Sep 2020 12:25:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Sep 2020 12:25:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AD7371804D8 for ; Thu, 17 Sep 2020 04:33:32 -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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from srv015.mail.ichtushosting.com (srv015.mail.ichtushosting.com [159.69.182.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 17 Sep 2020 04:33:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=stitcher.io ; s=default; h=To:Date:Message-Id:Subject:Mime-Version:Content-Type:From: Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=88jVtnovs7qzccSxX1WdnMd2GeN8AhC3Os1OP4zVURA=; b=JuWu1BmcLl0FGXX6RuOlTLQyT9 MKtr1cEDoCnBl1ZWTS+ddyBNpbwCINIvI+cd3FD99BN6jU/b2lP7uHamtqidvlSba7J4uonk5BIB+ 9TB7bYu0B4+3mxZCfKviM2SDwp8npD4xaRl/cE2u0g2FyNoOyHGNFgHwp8f3vqqTnEI6rkR/Sz9XP TlmA4+3n36c/uMxmck+DEg5LdhCstbtPzknCfd7rFy8XkGFuuIOGS4ybNjIHpn3vym6jobuCHX+xP sk7H9zaivLyDRRHuz7s8O6kNNgv2M3pcmjFLq6qHd4SP1hQbWbWExP648o3eV3G7lZCwedhrXyuL+ 2PmGeZBw==; Received: from srv020.mail.ichtushosting.com ([78.46.213.219]) by srv015.mail.ichtushosting.com stage1 with esmtp (Exim MailCleaner) id 1kIsAH-0008Gq-Vu for from ; Thu, 17 Sep 2020 13:33:27 +0200 Received: from ptr-fq9pjpieaqhcba5d2qq.18120a2.ip6.access.telenet.be (ptr-fq9pjpieaqhcba5d2qq.18120a2.ip6.access.telenet.be [IPv6:2a02:1812:c3c:3a00:995c:52ad:d34d:8c22]) (Authenticated sender: brendt@stitcher.io) by srv020.mail.ichtushosting.com (Postfix) with ESMTPSA id C7F513EBDA for ; Thu, 17 Sep 2020 13:33:24 +0200 (CEST) X-MailCleaner-return_path: brendt@stitcher.io X-MailCleaner-sender_address: brendt@stitcher.io X-MailCleaner-recipients: internals@lists.php.net X-MailCleaner-SPF: pass Content-Type: multipart/alternative; boundary="Apple-Mail=_0845F7F7-35E2-4E4D-ABEE-673383770EF9" Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.80.23.2.2\)) Message-ID: <09DE673B-71B7-4C8E-A795-900B51F8E476@stitcher.io> Date: Thu, 17 Sep 2020 13:33:23 +0200 To: PHP internals X-Mailer: Apple Mail (2.3608.80.23.2.2) X-MailCleaner-TrustedIPs: Ok Subject: The case for transpiled generics From: brendt@stitcher.io (Brent Roose) --Apple-Mail=_0845F7F7-35E2-4E4D-ABEE-673383770EF9 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 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 = the 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 = PhpStorm acknowledge their significance =E2=80=94 they will add built-in = support for both psalm and PHPStan later this year [4]. Rasmus Lerdorf = also showed interest 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.=20 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 transpile their code to plain old JavaScript, but it seems that = they are=E2=80=A6 fine 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: static 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 those features, and yet they have been proven so useful = and wanted over the last 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 = TypeScript - As with all things-PHP: it's opt-in. You don't have to use the syntax = if 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.=20 Kind regards Brent [1] https://wiki.php.net/rfc/generics = =20 [2] https://wiki.php.net/rfc/generic-arrays = =20 [3] https://github.com/PHPGenerics/php-generics-rfc/issues/45 = =20 [4] = https://blog.jetbrains.com/phpstorm/2020/07/phpstan-and-psalm-support-comi= ng-to-phpstorm/ = =20 [5] https://externals.io/message/101477#101592 = =20 [6] https://wiki.php.net/rfc/constructor_promotion#desugaring = =20= --Apple-Mail=_0845F7F7-35E2-4E4D-ABEE-673383770EF9--