Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92956 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96723 invoked from network); 29 Apr 2016 17:44:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Apr 2016 17:44:01 -0000 Authentication-Results: pb1.pair.com smtp.mail=mathieu@texthtml.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=mathieu@texthtml.net; sender-id=pass Received-SPF: pass (pb1.pair.com: domain texthtml.net designates 62.210.206.189 as permitted sender) X-PHP-List-Original-Sender: mathieu@texthtml.net X-Host-Fingerprint: 62.210.206.189 texthtml.net Received: from [62.210.206.189] ([62.210.206.189:48928] helo=texthtml.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C0/D2-26386-F5D93275 for ; Fri, 29 Apr 2016 13:44:00 -0400 Received: by texthtml.net (Postfix, from userid 65534) id 9F2CE2AF; Fri, 29 Apr 2016 17:43:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on b7ba30992c3e X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,HTML_MESSAGE autolearn=ham autolearn_force=no version=3.4.1 Received: from [192.168.1.70] (stunnel_mail_1.mail_default [172.27.0.8]) (Authenticated sender: mathieu@texthtml.net) by texthtml.net (Postfix) with ESMTPA id 87D1C2AD; Fri, 29 Apr 2016 17:43:54 +0000 (UTC) To: Rowan Collins , PHP internals References: <013101d19ff8$596b6010$0c422030$@tutteli.ch> Message-ID: Date: Fri, 29 Apr 2016 19:43:53 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:47.0) Gecko/20100101 Thunderbird/47.0a2 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------3F19F188D0618A129DE0C879" Subject: Re: [PHP-DEV] [RFC:generics] From: mathieu@texthtml.net (Mathieu Rochette) --------------3F19F188D0618A129DE0C879 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit On 04/29/2016 06:02 PM, Rowan Collins wrote: > Hi Dominic, > > Sorry, I think this may have got sent too early by mistake; please > ignore the duplicate! > > Also, it's crossed in the post with your next message, but I'll send > it anyway, in case it helps anyone understand. > > > Dominic Grostate wrote on 29/04/2016 16:45: >> It's not insane, it's liskov substitution. But that is an insane >> example, typical of Java to create a type that extends it's parent by >> adding value constraints.. directly violating the principle. > > > Let's translate the covariant example code into English: > > function addPiToList(List $list): void > // "I am a function that accepts any list of Numbers" > { > $list->add(3.1415); > // "To the list I was given, add the Double 3.1415, which is a kind of > Number" > } > > $li = new List; > // "This is a new list of Integers" > addPiToList($li) > // "You asked for a list of Numbers; Integers are Numbers, so I can > give you my list" > > So far, so reasonable. But now lets see what gets executed: > > $list->add(3.1415); > // "To the list of Integers you gave me, I will add the Double 3.1415" > // "Oops! A Double isn't allowed in your list, I will now produce a > Type Error" > > > > Now the same with the contravariant List example: > > function addPiToList(List $list): void > // "I am a function that accepts a list of any category, as long as > that category includes Doubles" > { > $list->add(3.1415); > // "To the list I was given, add the Double 3.1415" > } > > $ln = new List; > // "This is a new list of Numbers, it can mix Integers and Doubles, > because they are both types of Number" > addPiToList($ln) > // "You asked for a list that can contain Doubles; my list can contain > any Number, including Doubles, so I can give you my list" > > And inside the function: > > $list->add(3.1415); > // "To the list of Numbers you gave me, I will add the Double 3.1415" > // "Yay, it worked. :)" > > > Contravariance always seems counterintuitive, but it turns out it > really is the appropriate constraint in certain situations. indeed, thank you for this very detailed explanation. can this also be useful/makes sense for declaring a class? (eg: class List {}) maybe an abstract class ? or is it just for parameters ? > > Regards, -- Mathieu Rochette --------------3F19F188D0618A129DE0C879--