Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92943 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71703 invoked from network); 29 Apr 2016 16:00:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Apr 2016 16:00:18 -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.67 as permitted sender) X-PHP-List-Original-Sender: codekestrel@googlemail.com X-Host-Fingerprint: 74.125.82.67 mail-wm0-f67.google.com Received: from [74.125.82.67] ([74.125.82.67:33528] helo=mail-wm0-f67.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 55/4E-26386-30583275 for ; Fri, 29 Apr 2016 12:00:04 -0400 Received: by mail-wm0-f67.google.com with SMTP id r12so6025178wme.0 for ; Fri, 29 Apr 2016 09:00:03 -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=zyX+x6Pew5MJoeOIJWnTFQiN7HzDXEzeok3Jw1VO/mE=; b=es4LHzC6XX1RDjeXN+W3xdcL+LWy5aSor9gwGiYgY87pMigpQn0H1nNFaPOIYbU3tN ObIFcVwm+ihvmtL4m9DsBDc3YbeBHHwQNR3k8yPq0yuYLW0K50MhqPc15s8O6pd5b+Xu FJvlVVIvmrmgP8/2+VmQCBsPAXofZwrKoGyfvz6WKg0b50E6gKeqFOn6LHtsFZHRvtqT H5UWx+0Bc/GJBnv5vJseopidkiHr0+mCO2Kl+RCrN8ZuzoV5lstM1yHSXobMp0H3wUcx 7MpJIDwcVBf+7zxIyOziF2qFtVXwhUrNNE9gSJETqspEubMpubx6Cd6lNHp54cZYw2LQ B/pw== 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=zyX+x6Pew5MJoeOIJWnTFQiN7HzDXEzeok3Jw1VO/mE=; b=NZ8POlmXNg7e2gn172iwjwbMlQWuiLgY0lZadZABwYcSjWVnObloHxZvZvCYovY70q ajYelBf7yTWOYgzZ+R+ZBN2b8PiAAZvYkVRUiEb5eHP3rrYL6lJJOLapWjRk8DGE8k7C 0XhX9IEGdAQbbJUqFjm1f7UXtO+2t/y1Jhy9IfSdDxkVjqUGE9tAtY/Kf1RAlekqIuRe KzxNIMbMp+SEa+pLUfy6WwYW5YWhSQmrOv9y95buidh+6TG7yToDXK3olRrpE+gBY1RI Su/03VncnH0Aaqsr8dlyfRx6O/Ytw6SntkC+uiLG3OUnCGBzwmZ7JLA5sdMeIMnXn2Hm HQeQ== X-Gm-Message-State: AOPr4FUxTNHjAVJ1GmF+AnZCOMNgWhlAg+MtNO8M84OxkUMl9CdtT+/8N/BuwDAp4Hu1VGj2WkDG7rSuCbM9TQ== MIME-Version: 1.0 X-Received: by 10.28.30.148 with SMTP id e142mr4817377wme.69.1461945599834; Fri, 29 Apr 2016 08:59:59 -0700 (PDT) Received: by 10.28.140.10 with HTTP; Fri, 29 Apr 2016 08:59:59 -0700 (PDT) Received: by 10.28.140.10 with HTTP; Fri, 29 Apr 2016 08:59:59 -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:59:59 +0100 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: multipart/alternative; boundary=001a114b245a28c0d60531a1bc68 Subject: Re: [PHP-DEV] [RFC:generics] From: codekestrel@googlemail.com (Dominic Grostate) --001a114b245a28c0d60531a1bc68 Content-Type: text/plain; charset=UTF-8 After reading your email again, I find myself agreeing. This is because I don't consider List to be a subtype of List. LinkedList would be a subtype of List, but inheritance doesn't really extend any further beyond that, for the reason you have illustrated. The example you gave is similar to implementing an interface with a type hint of Number, but trying to changing it to Integer. The method signatures are clearly incompatible, so I believe the same applies here in generics. 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] > --001a114b245a28c0d60531a1bc68--