Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81910 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 61577 invoked from network); 5 Feb 2015 12:20:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Feb 2015 12:20:08 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.176 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.176 mail-wi0-f176.google.com Received: from [209.85.212.176] ([209.85.212.176:64451] helo=mail-wi0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C3/C6-27691-8FF53D45 for ; Thu, 05 Feb 2015 07:20:08 -0500 Received: by mail-wi0-f176.google.com with SMTP id bs8so38583083wib.3 for ; Thu, 05 Feb 2015 04:20:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=STz88dS0Hz5J9zQTvuBnTjcYGoh/1G5m4A7diTIq5gc=; b=Mwaz9LCn2HBP3q5cfRUqws8SO1LPvRr04+n1UjACB9Z5TzxT5vUZQex8UdZxhieAFl QBgIfIBDWIwxxHxPUl6e5ZUWS3+313YEIbqDqEZjGfFquQSW1MXn/7XjrsTcm98vUykB a9n16cixputCPibxCI0cazAlkDB2IFnbI/4tg2w8y+lHoAo2IjIo1Gw1mhXR3nwwBAAu L9xFILhOCb+gUZsk6MwTT6iTWit0NtuFIc0wb/Xnv1Yct5HpcD9OBVKoPQ6i82jkcYL9 LBOsVGnOljfuXBkI2Bo9TtNUPls+sA/NLnA/dM4Nu1Bgf8PcEfHesmHyJMhZhUiOxL3Z UeBw== X-Received: by 10.194.79.226 with SMTP id m2mr7328108wjx.60.1423138805439; Thu, 05 Feb 2015 04:20:05 -0800 (PST) Received: from [192.168.0.172] ([62.189.198.114]) by mx.google.com with ESMTPSA id li7sm33663366wic.4.2015.02.05.04.20.04 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Feb 2015 04:20:04 -0800 (PST) Message-ID: <54D35FEC.7080902@gmail.com> Date: Thu, 05 Feb 2015 12:19:56 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Allow dropping typehints during inheritance From: rowan.collins@gmail.com (Rowan Collins) Hannes Magnusson wrote on 04/02/2015 23:58: > On Wed, Feb 4, 2015 at 10:49 AM, Nikita Popov wrote: >> Hi internals! >> >> 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. > So what it supports "more inputs"? > It does constitute an LSP violation. "more inputs" is not what the > guarantee is at all, if that is what you want you'd typehint on a > interface. Have a look at the Wikipedia article on covariance and contravariance; the idea is that if you know you have a container which can contain Cats, a custom implementation that also happens to accept Dogs is fine, but an implementation that only accepts Kittens is not, because the object is contractually required to accept any Cat that you give it. Return types are the reverse, because the object is contractually required to return you nothing but Cats, so returning a Dog would be a violation; but constraining itself to only return Kittens doesn't affect the contract you have with it. The special case proposed here is that the lack of type hint means "any type", as though you were type hinting the base of all possible types. Thus removing a typehint on a parameter does not allow the object to violate the parent's contract, and nor does adding a return typehint. However, the fact that this is tricky to understand is probably a big problem for it. The Wikipedia article notes that few languages implement parameter contravariance, mostly because they'd have to mix it with type-based function overloading, making real-life examples hard to find. Regards, -- Rowan Collins [IMSoP]