Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101360 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59238 invoked from network); 18 Dec 2017 14:45:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Dec 2017 14:45:59 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.53 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 74.125.82.53 mail-wm0-f53.google.com Received: from [74.125.82.53] ([74.125.82.53:41896] helo=mail-wm0-f53.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4D/16-21958-3A4D73A5 for ; Mon, 18 Dec 2017 09:45:56 -0500 Received: by mail-wm0-f53.google.com with SMTP id g75so29790945wme.0 for ; Mon, 18 Dec 2017 06:45:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=VUrKTxXqdfZfr8PCGXdPvebIgBTFdreSDzYA51qE2d4=; b=bvCITH2/ah9ChHpw+DbKu45yA/rGC/MJwuN2TkPh9kX+uualgbKjMuA6wL0Z7CvnGf jMi0ogpFgC3l9FW3Q1GgIjRK+oZVOFNG57LdW3nPxukvrjWXDbNYQgWQRVzJUpy08cnN xWFlN3nEW2dBfYE+JlpFcvXUFZj15VOsIHeoPs9uVjg2AjuHEQwkmZEWtX7nyt5aZ3Zf TTvldgS8EFTdl9XeovurD1Zj9wbzrFh7jJ1O5QFE5rCcn09Jlp//38Nn84Qdmzua0aX2 wHRhHnAmAT5tpSCKPiUtwHYSBnN2Oz2OlPC9S5JUtZxL0v4rdS9bKDYFzcQj/36hPCaT XMPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=VUrKTxXqdfZfr8PCGXdPvebIgBTFdreSDzYA51qE2d4=; b=jryl6Ht4v2efIsjVIpTbHLq004VBcUO89WeRVWQJi6gjqTyKzthipr0jKfQFvcvlwf cImwucxO13bG3dhyKkBebUfguyLVe8ReiSt8VxD6f+aVMJIr9tHsN1s28KXIX0m/ZPaT x6BB61AcicncBFH1f7Ji/EdUDRkaS1lfc5IlmUCAsIiUB6lJmDAlRlI1LE7sqycNPzUE hMyopsp4a0zKy3cNGbrsGG7+CL2KUHoMRCPzuB1Qky817MTySyiOf1r6UIe/NXQfhf9J 6WbkWe6aVvlqqnyjgWs1I9W6pysasSu31wttIGqDebOLh0v3dl/nXNvHPP8JT8xU8bSa ShSQ== X-Gm-Message-State: AKGB3mIlxKfBLlyrqlt64SriNUjh5tkTB4AmmW/IbgcYX3nGt6LSI9CM Sd1GJNqcFfwuC/cJRN7QZKyEaxTwh/LiGhI2FF0TRe48 X-Google-Smtp-Source: ACJfBosLsX7wj0KwTO46rg4/MR5DR4ZEXs9yVp6MmaLnJyPzumARxDfTUlMlpraK0HECd54gK03uXwBnNZe291Pvc6Q= X-Received: by 10.28.229.213 with SMTP id c204mr23954wmh.57.1513608352701; Mon, 18 Dec 2017 06:45:52 -0800 (PST) MIME-Version: 1.0 Sender: morrison.levi@gmail.com Received: by 10.28.21.68 with HTTP; Mon, 18 Dec 2017 06:45:52 -0800 (PST) In-Reply-To: References: Date: Mon, 18 Dec 2017 07:45:52 -0700 X-Google-Sender-Auth: wqP8pYudz_KSgGPqbF6RUfJFcBU Message-ID: To: Andreas Hennings Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Covariance, again From: levim@php.net (Levi Morrison) On Mon, Dec 18, 2017 at 5:18 AM, Andreas Hennings wrote: > There were discussions about covariance and contravariance in the past. > https://externals.io/message/98085#98105 > Unfortunately I was not subscribed back then, so I cannot respond to anything. > So, here it goes again. > > WIth co- and contravariance, the following would be possible: > - contravariance.php - https://3v4l.org/I3v0u > - covariance.php - https://3v4l.org/i79O5 > > (from guilhermeblanco's older email in "PHP's support to > contravariance and covariance") > > The main problem was expressed by Levi Morrison in this older thread. > > Currently we do not autoload classes in type hints. > https://3v4l.org/sFsDd > In the example I can declare "UnknownClass" as a return type hint, and > PHP won't care. > > However, to validate if a return type matches with the parent > definition, the class must be autoloaded first. > Or rather: > - If the return type is identical with the parent, PHP can say "yes" > with no class loading required. > - If the return type is different from the parent: > -- Currently, PHP simply says "no" (Fatal error: Declaration of > C::foo(): C must be compatible with I::foo()). > -- To support covariance, PHP would have to autoload the class in the > type hint, and then check the hierarchy. > > > ## Solutions proposed in old thread > > Levi Morrison: > >> You need to adjust the passes over the code to register symbols and >> their declared relationships, and then in a separate pass validate >> them. After that if the class isn't found then you trigger an >> autoload. >> >> It's doable, it just hasn't been done. > > Christoph M. Becker: > >> An alternative might be forward class declarations: > > > ## What I propose instead > > I think it is not so complicated actually, and can be done without BC > break, and without forward type hints. > This only gives us covariance. Contravariance is another story. > > When a class declaration is executed, do the following: > - Parse the class AST (obviously). > - Autoload all identifiers in "extends" and "implements". > - Autoload all identifiers in return type hints that are not identical > with the parent return type hint. > > If such a class is not found, report "Return type must either be > identical with the parent, or it must be an existing or autoloadable > class or interface." > Well, or any message that clarifies why this one was autoloaded, while > other type hint classes are not autoloaded. > > So, this behavior is the same as today, except for the case of return > type hints that differ from the parent, which currently result in > fatal error. > > Does this sound doable? Am I missing something else? I believe your algorithm fails on this simple setup: If I correctly typed this from memory there is no way to order this such that all units are defined ahead of time as needed for verifying correctness. This means we trigger the autoloader even though the type is defined in the same file. Even if we do some more complicated compile-time passes we'd fail on things like this: This case shows that care needs to be taken to get the order down correctly even if we autoload: All-in-all I don't think we can resolve every case cleanly because we do not have purely ahead-of-time compilation for all units involved. I think every method of implementing this feature has drawbacks and we need to thoughtfully evaluate them.