Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111889 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16857 invoked from network); 17 Sep 2020 18:19:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Sep 2020 18:19:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 376E8180598 for ; Thu, 17 Sep 2020 10:28:30 -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,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 10:28:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=stitcher.io ; s=default; h=To:References:Message-Id:Content-Transfer-Encoding:Cc:Date: In-Reply-To:From:Subject:Mime-Version:Content-Type:Sender:Reply-To:Content-ID :Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To: Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe :List-Post:List-Owner:List-Archive; bh=xhFwnPE7dKLYD/mxH/pChfrnUXO4S/CGr4Wimdj06uw=; b=R4+kXHCFAZ0RxKnqzyXQZsrJM3 cH3sMLo4ldXnmQ8uY8lkBk45pmpB2t5D3oeyy+4FFNP3ZCsOG/jOGz5TaFoum0887kWDQe1ALu5ni GOnoUxOhWgECu/NsOty3zdtxgnGSMfyJgQtpS2Z7tYXlxpUP7vTmUJ7zgPaP+pKcbXxBz7DCp/mLl CppQXP1pnW5bdnqjmQCd5Lunhb0sLZonPoCfm1QhGAyGHf+LX0C7Xm2JZsYzjT0RqyS4aEQBtzrMS i/72BRYF/bQw1SM17eevmJbZQJEX6H6IbH67oWQFV0DztJRTChjJsc6IZUDglAClUDmOzbWOSx006 DVR5J99w==; Received: from srv020.mail.ichtushosting.com ([78.46.213.219]) by srv015.mail.ichtushosting.com stage1 with esmtp (Exim MailCleaner) id 1kIxhs-0007bq-PZ from ; Thu, 17 Sep 2020 19:28:28 +0200 Received: from ptr-fq9pjpjoj2u95w9lb54.18120a2.ip6.access.telenet.be (ptr-fq9pjpjoj2u95w9lb54.18120a2.ip6.access.telenet.be [IPv6:2a02:1812:c3c:3a00:edce:f36d:6feb:e4a8]) (Authenticated sender: brendt@stitcher.io) by srv020.mail.ichtushosting.com (Postfix) with ESMTPSA id 41E553EBDA; Thu, 17 Sep 2020 19:28:28 +0200 (CEST) X-MailCleaner-return_path: brendt@stitcher.io X-MailCleaner-sender_address: brendt@stitcher.io X-MailCleaner-recipients: larry@garfieldtech.com, internals@lists.php.net X-MailCleaner-SPF: pass Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.80.23.2.2\)) In-Reply-To: <4608608e-906c-45d2-9f7e-e7f4213aa994@www.fastmail.com> Date: Thu, 17 Sep 2020 19:28:27 +0200 Cc: php internals Content-Transfer-Encoding: quoted-printable Message-ID: References: <09DE673B-71B7-4C8E-A795-900B51F8E476@stitcher.io> <4608608e-906c-45d2-9f7e-e7f4213aa994@www.fastmail.com> To: Larry Garfield X-Mailer: Apple Mail (2.3608.80.23.2.2) X-MailCleaner-TrustedIPs: Ok Subject: Re: [PHP-DEV] The case for transpiled generics From: brendt@stitcher.io (Brent Roose) Hi Matthew and Larry > On 17 Sep 2020, at 18:21, Larry Garfield = wrote: >=20 > On Thu, Sep 17, 2020, at 10:21 AM, Matthew Brown wrote: >> Quick thing before I get into my own reaction: >>=20 >> Transpiling is normally thought of as the process of converting one >> language into another. Tools like Babel transpile TypeScript to = JavaScript. I looked up the definition [1] before writing my mail and it said=20 > A [=E2=80=A6] transpiler is a type of translator that takes the source = code of a program written in a programming language as its input and = produces an equivalent source code in the same or a different = programming language But I don't want to get stuck on phrasing, if elidiing is the right term = as Larry suggests, let's go with it! >>=20 >> What's being proposed here (AFAICT) is type erasure =E2=80=93 the = generic type >> information would be erased during the conversion to opcodes. >>=20 >> Python (the language) has type erasure for all its types (including = its >> generic types), so no popular Python interpreters check that a = function >> call's argument types match up with the function signature. I didn't know about Python's approach before, and I think it's = validating the idea, it's at least something worth looking into. >>=20 >> PHP currently does not erase any types so the opcodes generated by = PHP's >> interpreter include type checks for all the arguments passed to a = typed >> function signature. >>=20 >> Hack follows PHP's model, but erases generic types by default (though = has >> more recently introduced the concept of reified generics) in much the = same >> way you're proposing. >=20 > I concur; this is type elidiing, not transpiling. Transpiling implies = a user-triggered pre-run step. I know Sara has mused about it before = but it's not happened yet. >=20 > Basically what is being proposed (for those who need a concrete = example) is you'd write this: >=20 > class Collection { >=20 > public function add(T $item) { ... } >=20 > } > $c =3D new Collection(). >=20 > And linters would recognize that, and it would parse, but at runtime = it would compile to: >=20 > class Collection { >=20 > public function add(mixed $item) { ... } >=20 > } > $c =3D new Collection(). >=20 >=20 > And that's the opcodes that would be saved. (I'm hand-waving a lot = here.) >=20 > I would be on board with this, although I would ask if it's possible = to provide some compile time checks; I'm thinking LSP validation, which = is still relevant for inheritance with generics. Beyond that, leave the = runtime to not enforce it. (I have no idea how viable that is, but Sara = seemed to think it would work so it probably=20 I agree that's an approach worth looking into. I didn't want to discuss = specifics yet, and first get a feel of what people think of the general = idea, but it's good that you're mentioning this, and I'm glad others are = open to the idea. >=20 > Another possibility could be to desugar that original to: >=20 > class Collection { > public $__T; >=20 > public function add(mixed $item) { > if (!$item instanceof $this->__T) { > throw new TypeError(); > } > } > } > $c =3D new Collection(). > $c->__T =3D=3D 'Product'; >=20 > Which is essentially what you can do in userspace today, but = automated. I don't know how viable that is, but it's another thing to = consider. >=20 > --Larry Garfield >=20 > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >=20 Kind regards Brent [1] https://en.wikipedia.org/wiki/Source-to-source_compiler