Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81843 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 9286 invoked from network); 4 Feb 2015 22:03:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Feb 2015 22:03:38 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.173 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.173 mail-we0-f173.google.com Received: from [74.125.82.173] ([74.125.82.173:35020] helo=mail-we0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 28/8C-40930-93792D45 for ; Wed, 04 Feb 2015 17:03:37 -0500 Received: by mail-we0-f173.google.com with SMTP id w62so4375493wes.4 for ; Wed, 04 Feb 2015 14:03:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=user-agent:in-reply-to:references:mime-version :content-transfer-encoding:content-type:subject:from:date:cc :message-id; bh=KdKXeHu9mus8Xh+CW2Cfqh1NAOymZDhYAWxXPiypMSo=; b=FK26Ib7dpv/WZ283nyi3405QyqkTPzCrCqKe5k4fTU14Nz+2IY0LqXkazD0RLgOzrc YnXq1sErnLbvpi8UQOW76J6FKBqkw4mDEz5Msp2GvLD9H/IBzCj20muvGpVb3mIxqMr0 BxFGrC3ZfoVuIvlo9Hh48NOKT2V3LybIEwaoHHlauigtQrSquNtZdEIQPj8ASI1nnq9o KkT8ApQYS2f3UJNWWaSPsPeQME/VGhKX/Q5gTGmGHn3Qo4kJ5Zx/e8/3Sw0D6MPieHpL sYLTtwlWu7XlpJxXOjrjktc6YJaTTErxpp/i2cP8rdxnAbDmqoGEOe5xHBiNOvyb+tXr UzKA== X-Received: by 10.194.143.109 with SMTP id sd13mr1123204wjb.70.1423087414055; Wed, 04 Feb 2015 14:03:34 -0800 (PST) Received: from [192.168.0.3] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id vq9sm4525230wjc.6.2015.02.04.14.03.33 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 04 Feb 2015 14:03:33 -0800 (PST) User-Agent: K-9 Mail for Android In-Reply-To: References: <73F87BE7-E57C-4275-A435-98C39A751E87@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Date: Wed, 04 Feb 2015 22:02:30 +0000 CC: PHP internals Message-ID: <7D2B5777-C636-43B2-8C17-284F407295C5@gmail.com> Subject: Re: [PHP-DEV] Allow dropping typehints during inheritance From: rowan.collins@gmail.com (Rowan Collins) On 4 February 2015 21:25:49 GMT, Nikita Popov wrote: >On Wed, Feb 4, 2015 at 10:17 PM, Rowan Collins > >wrote: > >> On 4 February 2015 21:02:30 GMT, Yasuo Ohgaki >wrote: >> >Hi Nikita, >> > >> >On Thu, Feb 5, 2015 at 3:49 AM, Nikita Popov >> >wrote: >> > >> >> Currently we do not allow [1] removing a typehint during >inheritance. >> >For >> >> example the following code is not valid: >> >> >> >> interface A { >> >> public function method(Typehint $param); >> >> } >> >> class B implements A { >> >> public function method($param); >> >> } >> >> // Fatal error: Declaration of B::method() must be compatible >> >with >> >> A::method(Typehint $param) >> >> >> >> The above code does *not* constitute an LSP violation, because >> >B::method() >> >> accepts more inputs than A::method(). However we still forbid it. >> >> >> >> This is an issue, because it makes it impossible to add typehints >to >> >> parameters at a later point in time. I've seen this issue come up >> >both in >> >> userland code, as well as in a recent DateTime change, see >> >> >> >> >> > >> >https://github.com/php/php-src/commit/8e19705a93d785cd1ff8ba3a69699b00169fea47 >> >> . >> >> >> >> Instead of reverting the DateTime BC break, I'm wondering if it >> >wouldn't be >> >> better to fix the root cause by making the inheritance check less >> >strict >> >> and allow removing typehints? >> >> >> > >> >I can understand your reason. It's reasonable perfectly. >> >Template is better, but PHP is weakly typed language. >> >I think it's acceptable. >> > >> >Since Dmitry agreed to introduce DbC, if he like the syntax, etc and >> >proposal is passed. >> >DbC may be used to check various types or user may simply write code >> >that >> >handles >> >various types in function body. >> >> This is actually my fear: that people will misunderstand this as an >excuse >> to write invalid type checks, such as ones which are tighter rather >than >> looser than the parent class (it takes a bit of careful thought to >> understand why this is wrong). Unfortunately, we don't have a keyword >for >> explicitly allowing any value, or even a base for all objects, so I >guess >> we have to take that risk, but at least if contravariance were fully >> supported, we could have clear examples of correct usage. >> >> (Incidentally, I'm not sure how templates are relevant - this is >purely >> about inheritance and LSP of ordinary objects.) >> >> >Maybe I wasn't clear here. I do not propose to allow any code that >violates >LSP. You will not be able to add a parameter typehint if the parent >doesn't >have one and you will not be able to change a typehint to a different >class. I'm only proposing to support parameter contravariance to the >limited degree we are capable of implementing currently. I was replying to the idea that a "user may simply write code that handles various types in function body". While a savvy user would only use that to implement contravariance, encouraging it as a possibility risks people implementing covariance or other weirdness because they haven't understood why it's a bad thing to do. Full support for contravariance wouldn't stop that being possible, but it would give much better examples for people to learn from. Regards, -- Rowan Collins [IMSoP]