Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:111886
Return-Path: <larry@garfieldtech.com>
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 7170 invoked from network); 17 Sep 2020 17:13:30 -0000
Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5)
  by pb1.pair.com with SMTP; 17 Sep 2020 17:13:30 -0000
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id 08419180564
	for <internals@lists.php.net>; Thu, 17 Sep 2020 09:22:11 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,
	DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,
	SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2
X-Spam-Virus: No
X-Envelope-From: <larry@garfieldtech.com>
Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25])
	(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 <internals@lists.php.net>; Thu, 17 Sep 2020 09:22:10 -0700 (PDT)
Received: from compute7.internal (compute7.nyi.internal [10.202.2.47])
	by mailout.nyi.internal (Postfix) with ESMTP id 029E75C0110
	for <internals@lists.php.net>; Thu, 17 Sep 2020 12:22:10 -0400 (EDT)
Received: from imap26 ([10.202.2.76])
  by compute7.internal (MEProxy); Thu, 17 Sep 2020 12:22:10 -0400
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
	messagingengine.com; h=content-transfer-encoding:content-type
	:date:from:in-reply-to:message-id:mime-version:references
	:subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender
	:x-sasl-enc; s=fm3; bh=g4/LTnJW2db7tmr4G1ycxZTUprtv2aHjNCJscvW61
	0A=; b=omVCRLVnhDzeHk/0JuFpcCbajWtleUzHZ+mFfeAU8Bsf0BUaFHkekCNt3
	bOJV96492nyF+1LmgdPK535iUa9U/dH0GfeFuhxAqCVtuhwjmqp7ZocbBu/DIhnL
	TKWTKRZKp7nIFV6bkmxieuUFBX5GkGwH/X35a1yWmruBuBfYmYgeHtkccLM2UKTN
	Nlp3kwwm3xNINm5oGAN7XuE39vlj0iAfaQiyc0YSoxKXXE88hY1ZUEw6hScdMaa4
	oS8XR3RwKlOdaE1GuUAv9LjzTgc9iMHnQBZI4vTUJED8S2OZyiaODWBEJClL/0e/
	9B+vuez71kZgZ3EV6WCcLjLBj3hYA==
X-ME-Sender: <xms:MY1jX_pBl7FJHbpoh8qwi5HSKGufcydse42GptrC3u5PJ-KXCE3t_w>
    <xme:MY1jX5rPG2kQTa9vQD9qL-rTxjSmfF-XADcoSsneGADQdvwwzZObChFAWyWaK-RJ8
    6Vx0BX04SmoFg>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrtdeggddutddvucetufdoteggodetrfdotf
    fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen
    uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne
    cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr
    rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg
    homheqnecuggftrfgrthhtvghrnhepffffffejffdugfegvedviedttedvgfejffefffej
    leefjeetveehgefhhfdvgfelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe
    hmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm
X-ME-Proxy: <xmx:MY1jX8MYtKLCeGisXRCAavmclAMDIsJ4SoxioWakAQkyLElDRSWd0g>
    <xmx:MY1jXy4plUeDOzgP6C_p7OdchfrqqcoZas5TVIG0cNLZw5AxwsAiLg>
    <xmx:MY1jX-6gLZRQeivGTa5kUhyLh6ZRicDmjVYjmnzwohxv8JNBUBBuvA>
    <xmx:MY1jX9J4RJyarrMmkPiHbLwQe0DtKKBBIV0d1R_FdFz-MU5_p5BZiA>
Received: by mailuser.nyi.internal (Postfix, from userid 501)
	id 860DE14200E8; Thu, 17 Sep 2020 12:22:09 -0400 (EDT)
X-Mailer: MessagingEngine.com Webmail Interface
User-Agent: Cyrus-JMAP/3.3.0-324-g0f99587-fm-20200916.004-g0f995879
Mime-Version: 1.0
Message-ID: <4608608e-906c-45d2-9f7e-e7f4213aa994@www.fastmail.com>
In-Reply-To: 
 <CAKuGYLSLm0aFVXdxcywzTAqbaohE--4zaaqFg9HYpDgQv6o=Pg@mail.gmail.com>
References: <09DE673B-71B7-4C8E-A795-900B51F8E476@stitcher.io>
 <CAKuGYLSLm0aFVXdxcywzTAqbaohE--4zaaqFg9HYpDgQv6o=Pg@mail.gmail.com>
Date: Thu, 17 Sep 2020 11:21:48 -0500
To: "php internals" <internals@lists.php.net>
Content-Type: text/plain;charset=utf-8
Content-Transfer-Encoding: quoted-printable
Subject: Re: [PHP-DEV] The case for transpiled generics
From: larry@garfieldtech.com ("Larry Garfield")

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 JavaSc=
ript.
>=20
> What's being proposed here (AFAICT) is type erasure =E2=80=93 the gene=
ric type
> information would be erased during the conversion to opcodes.
>=20
> Python (the language) has type erasure for all its types (including it=
s
> generic types), so no popular Python interpreters check that a functio=
n
> call's argument types match up with the function signature.
>=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 type=
d
> 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.

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.

Basically what is being proposed (for those who need a concrete example)=
 is you'd write this:

class Collection<T> {

  public function add(T $item) { ... }

}
$c =3D new Collection<Product>().

And linters would recognize that, and it would parse, but at runtime it =
would compile to:

class Collection {

  public function add(mixed $item) { ... }

}
$c =3D new Collection().


And that's the opcodes that would be saved.  (I'm hand-waving a lot here=
.)

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 ru=
ntime to not enforce it.  (I have no idea how viable that is, but Sara s=
eemed to think it would work so it probably would.)

Another possibility could be to desugar that original to:

class Collection {
  public $__T;

  public function add(mixed $item) {
    if (!$item instanceof $this->__T) {
      throw new TypeError();
    }
  }
}
$c =3D new Collection().
$c->__T =3D=3D 'Product';

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.

--Larry Garfield