Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101359 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 53543 invoked from network); 18 Dec 2017 12:19:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Dec 2017 12:19:10 -0000 Authentication-Results: pb1.pair.com header.from=andreas@dqxtech.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=andreas@dqxtech.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain dqxtech.net from 209.85.215.49 cause and error) X-PHP-List-Original-Sender: andreas@dqxtech.net X-Host-Fingerprint: 209.85.215.49 mail-lf0-f49.google.com Received: from [209.85.215.49] ([209.85.215.49:45179] helo=mail-lf0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7D/95-21958-832B73A5 for ; Mon, 18 Dec 2017 07:19:07 -0500 Received: by mail-lf0-f49.google.com with SMTP id f13so17497096lff.12 for ; Mon, 18 Dec 2017 04:19:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=HTuj7POV97eyaeldkqAl/kHJDHOY3EfYRjE9p50/I+4=; b=jrt0jpgPKpc7RQPZyBGi/RhumJWgngp2dbugHyVPQRfzyYxJ8XdpiFpVgDOn0uCOvr FhOpgrZAPmEQmtKI9F6/8VzPJezVqzgKwR6/9k9N27gZ0M35G8+rzDm3KxO3B+NUyIV7 Nb6tbsIhPCr1nZYrRs488mvcTLTHj9YC7tk38pooyOWBd+5WPLM0aXf8+/j6owgOnxiT Qu6WBjYuPW1D4s2FonnY4Vtv+OPTQbyi7qN/3xfXSTvuNvHJ8u7J9de0btMhO5Yk5M1V v9L89Z3RipELN+OOJusrb5l7PDZT4K7tX02G2G50z36mMmb98aFshldcY7N0zB5/KVsr ZM/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=HTuj7POV97eyaeldkqAl/kHJDHOY3EfYRjE9p50/I+4=; b=gzO/GOofxOAq7pbp0k+P1g/JuzA+ssvl42UPSOJlwnOYpG/PR/f5suPvRaWRbm7bc9 peXQZxc0yaGomw0Vuxkz7r1M0XQKBeNmCVy9rJhI5nIDCAwI+8e3BGoJ9pAZAlLkAtdM 8X/A9ajDbaH2GJEhcCQv6mcWoY7sPRv2g5fML2J2L8Bg5C/fBpTpUmDofIEeFZqJODt5 DtHhqy6czDZylniVrqS7Sp2L2o7nCQstjTyDBLsuInqj1H1iA4n26j4m7zd5rcSDf7oI S/d/fGFo9puEDeAGLdFWMPR5ZPbPr4Qb5VEli/EuqUJaM1k1bv3lKC5SnPznepl10xe2 is9g== X-Gm-Message-State: AKGB3mJWu6yIp/U7Cmz/sAEtL5azimyAgM6Z2dPpAF2nmSh+I3tJRqeJ xnsWbpMcZoayTGHY3uVdkrGWm3H3 X-Google-Smtp-Source: ACJfBouX8UQsm73eihu4O2gkxBfx9RZj73p9ZxP5gcJN2b12GPO6zk5Dd01+fCJNVoeJY4FGavh25g== X-Received: by 10.46.23.20 with SMTP id l20mr12566677lje.25.1513599541508; Mon, 18 Dec 2017 04:19:01 -0800 (PST) Received: from mail-lf0-f47.google.com (mail-lf0-f47.google.com. [209.85.215.47]) by smtp.googlemail.com with ESMTPSA id h26sm2726106lfb.27.2017.12.18.04.19.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Dec 2017 04:19:00 -0800 (PST) Received: by mail-lf0-f47.google.com with SMTP id y78so11866328lfd.1 for ; Mon, 18 Dec 2017 04:19:00 -0800 (PST) X-Received: by 10.46.0.218 with SMTP id e87mr11768154lji.65.1513599539674; Mon, 18 Dec 2017 04:18:59 -0800 (PST) MIME-Version: 1.0 Received: by 10.25.170.16 with HTTP; Mon, 18 Dec 2017 04:18:38 -0800 (PST) Date: Mon, 18 Dec 2017 13:18:38 +0100 X-Gmail-Original-Message-ID: Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Covariance, again From: andreas@dqxtech.net (Andreas Hennings) 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? -- Andreas