Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98089 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28210 invoked from network); 31 Jan 2017 19:30:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Jan 2017 19:30:35 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.216.171 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.216.171 mail-qt0-f171.google.com Received: from [209.85.216.171] ([209.85.216.171:32790] helo=mail-qt0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E5/1A-51557-7D5E0985 for ; Tue, 31 Jan 2017 14:30:33 -0500 Received: by mail-qt0-f171.google.com with SMTP id v23so243628396qtb.0 for ; Tue, 31 Jan 2017 11:30:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=TWdKnVXUyJy7qi8fPn92f9UlIkdf8k0qM03EP9B/OAo=; b=AqOo8nY3DixIX0m4MsnGk4wEF4T7X3O8xdJG8s0d4xP2fTUmwNvEQUtWaJ24HivawA KOmdnBmFl+ZVDTiYOs7qAhki/vT5QBSVrZ64hearvasbBK8cVtmBTu56HLYMZC8qBGmb hyYF9UHqzdtxz7+EwbfX+Qftg4TD+fyB7c3YHx4oawr2OWrU85OYDlGSeqBYg3SnxYXq xdcPDaAKQkcMaSrBtymqnBRFquYMfzJNaI/EkAEmkvo8LkFqDFes2z7hCYv/QGWNcyiz k2Od/RNfnjERtBu5a2aX2HSwaPscdLRT/kXOeEqY/av8BX3yo1GyH/xylfJFp95Wlmfx wZAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=TWdKnVXUyJy7qi8fPn92f9UlIkdf8k0qM03EP9B/OAo=; b=PyCuoXa4/L/sZm5QAaUrYND3XconGlDSz7pawBEMPoTV+STOn85EC2bOkibm6UDiMU 4P1I8wYV6C9dPBvih64RAIvKuxI2KEZ50MhIjePRfJfUDfyQwDClO9/iGqtY5rTIv7wS 8LCvmCCzrgMMLIJmCdblszlvgtVns/0NEh5XEizh8akEzQhD7VOFgNcFXGU0NHczlAaL O7anTaE/oF0O6ZOMjnV3y7EcfJz2+KEDKAfLnIH6qjHlFhbfm8BlEM4EgWf1bwCDyfcF 3Dk0mso+UtZKnTKUz/pgHMvGFIXAirv+LwIzoNjV9GcVfpf/ScVBoisqrtsTtHoZpU4h t0XQ== X-Gm-Message-State: AIkVDXLU3gply6wMN2WhmaC4mqCb4Zh0r3w4CIIsv+XxlTSyiu5qlSp/1bGuNFJOzzmPrMSS5mscxBB7uRLrwQ== X-Received: by 10.200.50.97 with SMTP id y30mr28431866qta.194.1485891029265; Tue, 31 Jan 2017 11:30:29 -0800 (PST) MIME-Version: 1.0 Received: by 10.237.47.231 with HTTP; Tue, 31 Jan 2017 11:30:28 -0800 (PST) In-Reply-To: References: Date: Tue, 31 Jan 2017 20:30:28 +0100 Message-ID: To: "guilhermeblanco@gmail.com" Cc: PHP internals Content-Type: multipart/alternative; boundary=001a11406074f9bd7c054768f604 Subject: Re: [PHP-DEV] PHP's support to contravariance and covariance From: rasmus@mindplay.dk (Rasmus Schultz) --001a11406074f9bd7c054768f604 Content-Type: text/plain; charset=UTF-8 We were talking about this exact issue at work today. Supporting this for interfaces would be particularly useful - if an implementation better than satisfies the requirements defined by an interface, it should be able to implement that interface. I'd be very happy to see an RFC for this :-) On Tue, Jan 31, 2017 at 7:30 PM, guilhermeblanco@gmail.com < guilhermeblanco@gmail.com> wrote: > Hi internals, > > During my regular open source work, I realized that PHP yet do not support > contravariance and covariance for user classes. > > Some examples that I found that could be really easy to implement and > useful to end user are highlighted here: > > - contravariance.php - https://3v4l.org/I3v0u > - covariance.php - https://3v4l.org/i79O5 > > For all those lazy people that don't want to open 2 new tabs to understand > what I mean, here's a more elaborate example covering both scenarios: > > > class A {} > > class B extends A {} > > class Foo { > public function bar(B $b) : A {} > } > > class Woo extends Foo { > public function bar(A $b) : B {} > } > > $w = new Woo(); > > Basically, here are the highlights: > - method arguments are ok to reference wider parameter types (and we just > approved the PR for Parameter Type Widening) - this is what we call > contravariant, as contra inheritance direction > - return types are ok to enforce strictness for subtypes - this is what we > call covariant, as Woo is a subtype of Foo > > I've walked through PHP source and it looks like the only needed place to > modify is > https://github.com/php/php-src/blob/master/Zend/zend_inheritance.c#L184, > which would require to be decoupled in 2 functions to check which direction > is should support (covariance or contravariance). > > Is there anything else that I am missing? I'm happy to write an RFC for > that... =) > > Cheers, > > -- > Guilherme Blanco > Senior Technical Architect at Huge Inc. > --001a11406074f9bd7c054768f604--