Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92957 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99269 invoked from network); 29 Apr 2016 17:46:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Apr 2016 17:46:55 -0000 Authentication-Results: pb1.pair.com smtp.mail=codekestrel@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=codekestrel@googlemail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 74.125.82.47 as permitted sender) X-PHP-List-Original-Sender: codekestrel@googlemail.com X-Host-Fingerprint: 74.125.82.47 mail-wm0-f47.google.com Received: from [74.125.82.47] ([74.125.82.47:36307] helo=mail-wm0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5B/63-26386-E0E93275 for ; Fri, 29 Apr 2016 13:46:55 -0400 Received: by mail-wm0-f47.google.com with SMTP id n129so36788609wmn.1 for ; Fri, 29 Apr 2016 10:46:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=TFO+/J1jyJguw9NEP6RtBHLTqicUDUue0UmXaxSAmRA=; b=P8oAKil30muglNJqddw1qsMYswrldHwaj8g8YF9FrJN6JUIwFqvBfroqEk2R/a8xdD tnmxxCg+GdwDzKtrxqRlJctGRWqmoTsEKbWiBSqbsEqDmO1cPkpHeMVW+oUoiu+fxTFp FED2HXiHiaMRBKQABl0lYe7OvgjW0+AvpNPeIkoEecQ8KQV4hHIgvnUnmNh+d8W7e2O7 VL+JoC2f5em0OggIhb5Vdxm+Sw8zqWplqi+0OR4M+qCJ4lZVLahRAFzV2t0lgmkcGIL6 1KGlek+Pb7Jb5NLznalzpP2tHoR/XNYGdxGHJ691+4QnG3mNo47XN8IB4JRj3GT4UNVX LLkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=TFO+/J1jyJguw9NEP6RtBHLTqicUDUue0UmXaxSAmRA=; b=UASS9KkhSzmG0k19n356eZNLwMhUdKYX/eVyA9Iu7Ch5AUQeBjWESzAMv28mACRSWo 2b89gA56OGvYG8vOiM+t9MTOqTjK6Hc+1/9XR/lrYovcp3r20xC1g0gjij2WNSQGqEoW tuqTJUCSQUX+KzhLsGZPsPK0Cjim+fku7mpkBO+TyPGSy2nWgr3SBszxyTcjL+EVw9PV DFPzX0S0SVSg8GjIIo9MM5gk6NbljUCKfsqBkKneg+EC5lqghnvkwQzYwF5NCTg5UJBF +hgK806yEIz74IuxHVYzp8nHzlgZNlRneTKY2WMjPP1XrIn3nqYxgkM3x/+l8A0R0xVr QgVg== X-Gm-Message-State: AOPr4FUGtD6XtYyrQ+VdpUbl+16pGMAfyYpM275AZTmKt5dtU54JEoWfISumcmRZ7PLNavU6FZU3WWFUZGfEeA== MIME-Version: 1.0 X-Received: by 10.28.133.14 with SMTP id h14mr5679366wmd.100.1461952012373; Fri, 29 Apr 2016 10:46:52 -0700 (PDT) Received: by 10.28.140.10 with HTTP; Fri, 29 Apr 2016 10:46:52 -0700 (PDT) Received: by 10.28.140.10 with HTTP; Fri, 29 Apr 2016 10:46:52 -0700 (PDT) In-Reply-To: References: <013101d19ff8$596b6010$0c422030$@tutteli.ch> Date: Fri, 29 Apr 2016 18:46:52 +0100 Message-ID: To: Mathieu Rochette , Rasmus Schultz Cc: Rowan Collins , PHP internals Content-Type: multipart/alternative; boundary=001a1145e6546057060531a33a97 Subject: Re: [PHP-DEV] [RFC:generics] From: codekestrel@googlemail.com (Dominic Grostate) --001a1145e6546057060531a33a97 Content-Type: text/plain; charset=UTF-8 Yes this makes perfect sense now. On 29 Apr 2016 6:44 p.m., "Mathieu Rochette" wrote: > > > 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 > > --001a1145e6546057060531a33a97--