Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92940 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65465 invoked from network); 29 Apr 2016 15:45:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Apr 2016 15:45:16 -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.66 as permitted sender) X-PHP-List-Original-Sender: codekestrel@googlemail.com X-Host-Fingerprint: 74.125.82.66 mail-wm0-f66.google.com Received: from [74.125.82.66] ([74.125.82.66:34516] helo=mail-wm0-f66.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0F/DC-26386-88183275 for ; Fri, 29 Apr 2016 11:45:14 -0400 Received: by mail-wm0-f66.google.com with SMTP id n129so5916516wmn.1 for ; Fri, 29 Apr 2016 08:45:12 -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=rYGJVtEIEbrhKN3r+oKJ/O4uDDXGnlgpOtgEU9tFRV4=; b=RtF778m2UOY+LeiOHq8C7VAdDKv7TXAhZb3qVfK3I2Qgwdk1BPnEGbho+1G7R8z4w7 eXc6D4ng3fTwO9Am7sDrkstFzU50YcIM6BUlVG2b/UUbEj6QR1r96df/2gVLufl0pp5V vC9pOSk02zK+YFsULY6Pn26GzFduFV9uO4w6qOaNHXuP554mUMaNcnLVeUp8Wlw+Hmey v3UiRSdhHGdvLfZqmn1hjIfEOlyq4ZDJlMkaV2EOW2AP+BBmDVP6nPXAx3o7/9rtQRxF VBr6Twfv0b0pATE9umr5Er5mLu6BXDkCH/aNQbvX2jBEBOsu1SZ5z0myg6aqbO07iM/v 0aRA== 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=rYGJVtEIEbrhKN3r+oKJ/O4uDDXGnlgpOtgEU9tFRV4=; b=R+AyS/fIIZb4/CZe1837KsRzgs46rl+7W9qiQI0otj9j9vRfRXBCGGhZqEIteEvmbd FYHWhiXPFpLDTxq61bNOrs/VOxptg0qLV9GKi+sHjfROubuveWkItHVlHdeiohW0zyrW G+KgeQc4a6+GLk2r6/nvHc8sVj3IVGoad1raj2J5CwXe1N+ZIZrvvhlTFCgCfgaDzkBT JPyJXKPG6KoB7wypHIVV2+kxQIsigR+b1E5aovEE96Q1P2vU4FbjmwxwHbYmFfaG0Ade N7MAXlm1O/NjeNfr/V2IZ14dfAUveZtfGrZgwG+rcirGSz2bv1+bq4JrXcIRoP8WAGHq VAww== X-Gm-Message-State: AOPr4FWQ2oAovqvQnu98TomKmv81O2hXUXYQxrmRbtW/9ixLsMGZ/aSkMU8mPde/9OCEgIz98fQdZ6/vuK9qkQ== MIME-Version: 1.0 X-Received: by 10.28.31.6 with SMTP id f6mr4735292wmf.69.1461944709070; Fri, 29 Apr 2016 08:45:09 -0700 (PDT) Received: by 10.28.140.10 with HTTP; Fri, 29 Apr 2016 08:45:08 -0700 (PDT) Received: by 10.28.140.10 with HTTP; Fri, 29 Apr 2016 08:45:08 -0700 (PDT) In-Reply-To: References: <3cc8a4c7-2640-11ae-a67b-06f909ac1e27@texthtml.net> <57173859.4080501@rochette.cc> <013101d19ff8$596b6010$0c422030$@tutteli.ch> Date: Fri, 29 Apr 2016 16:45:08 +0100 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: multipart/alternative; boundary=001a114b26e610c5fe0531a18713 Subject: Re: [PHP-DEV] [RFC:generics] From: codekestrel@googlemail.com (Dominic Grostate) --001a114b26e610c5fe0531a18713 Content-Type: text/plain; charset=UTF-8 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. On 29 Apr 2016 4:22 p.m., "Rowan Collins" wrote: guilhermeblanco@gmail.com wrote on 29/04/2016 15:26: > You can add subtypes of A to a List in Java. What List >> >means is that the list itself may be a list of any type, provided that >> type >> >is compatible with A. So if B extends A, List is compatible with >> List> >extends A>, and when reading items you can assume they will be compatible >> >with A (since B extends A) but you can't add an A (because it's actually >> a >> >list of Bs). >> > >> > >> > Wrong. This is documented here > https://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html > and > specifically states: > > To write the method that works on lists of Number and the subtypes of > Number, > >> >such as Integer, Double, and Float, you would specify List> >Number>. The term List is more restrictive than List> >Number> because the former matches a list of type Number only, whereas >> >the latter matches a list of type Number or any of its subclasses. >> > Forgive me for butting in if I'm completely wrong, but is the confusion here between *declaring an instance* and *declaring a parameter constraint*? I think the point is that you can declare variables as follows: List li; // only accepts Integer List ld; // only accepts Double List ln; // accepts any Number, even if it is in fact an Integer or Double instance But if you then define a function accepting a parameter: public static double sumOfList(List list) Now your argument has to be of type List; you can pass "ln" above, but not "li" or "ld". To relax the function's contract, you can write this instead: public static double sumOfList(List list) Now the argument can be a List or List, so "li" and "ld" are legal arguments. The reason the default case is to be invariant, rather than covariant, is explained on the blog post Jesse linked to: http://hhvm.com/blog/9215/covariance-contravariance-and-super-type-constraints In a nutshell, consider this function: function addPiToList(List $list): void { $list->add(3.1415); // for the sake of example, assume 3.1415 instanceOf Double } If we allow covariance, then we can run addPiToList(new List) and the type conditions are met; clearly this is not sane, and is going to result in an error somewhere. So we need to be stricter: function addPiToList(List $list): void But the actual contract we want here is "any list we can legally add a Double to", and if we get a List or a List<*>, it would work fine, so our check is too strict. What's needed is a way to declare our function as contravariant: function addPiToList(List $list): void Now we can pass in a List or a List, but not a List, and our contract holds nicely. It took me a while to get my head around, but I think the above makes sense... Regards, -- Rowan Collins [IMSoP] --001a114b26e610c5fe0531a18713--