Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92938 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62369 invoked from network); 29 Apr 2016 15:21:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Apr 2016 15:21:52 -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 74.125.82.51 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.51 mail-wm0-f51.google.com Received: from [74.125.82.51] ([74.125.82.51:38525] helo=mail-wm0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D5/6C-26386-D0C73275 for ; Fri, 29 Apr 2016 11:21:50 -0400 Received: by mail-wm0-f51.google.com with SMTP id g17so41885570wme.1 for ; Fri, 29 Apr 2016 08:21:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to; bh=aPkeTqYaiRn6kynYKlUvOslWVmpcVR/bEJMeMZhnYyA=; b=NZRDsIhzoexcU7LDZR++5GM0fcw3XIU6uNZg9Cxfnol7IfntbOzFWYJixgTD+SJ6K6 ocRBdDQz6q6qOyEIMkCliVVpwIuj6tGZDl/b4NYa9ETMMISPR7vBpd3g/TTCFG2987HX TdliT7tpdr4k4nOk8RtficwQkxLkqZbOnrsPFUM5fQJcXL5aPhfMokydfw5nckrYI7aa +49/0UJj0FqPy3oddmjYJliimFX34IuY4XvgCv8UksH2jPUSE9yKYeWQb3HYlGDvZsKA tdSAA5IBqDDvckLkBV0DMlKgERTfOfDSO1mgB2ibLwgjjK8Upe5Jp+0b55PB4v66XmSj CJOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to; bh=aPkeTqYaiRn6kynYKlUvOslWVmpcVR/bEJMeMZhnYyA=; b=fblsc4yO3smWvL7Kv+xl5FggCCZzH2VEFDwF/0qTU2DOLDRTrjqjkJCD2NrF8RyJqS /zdyvCUskCatgWODiYA+2EDVG41oI1MCkik2ys6dWwbkosB5AM87mWoi8HpZNYZCF/I2 arBxdkMyqYTsFv/YXH03OOeW2gaE4p45n1L5l5KdMgLpMB0/rkjWEaKdHBCI/bGpKEr1 vcrywsqUgU5AJeFAJN2jdAPY6eCHjgAB6VzRyYZC7my5DA+wdKEZbk5GcSX5JHiIvlwr 9sjAtzfSxhViDUMcD1HbeTZMNBajnbA8YmxTwsVXTw05hCatrXPbM04ZI5935pP8Txfz 4eyg== X-Gm-Message-State: AOPr4FX3QATxQKWnIbZRlNcXIGOVvd1lP9DZ/EhQTNSGLJhg/F3loHeFAI5pLDpNg1QJSA== X-Received: by 10.194.100.129 with SMTP id ey1mr22865456wjb.26.1461943306828; Fri, 29 Apr 2016 08:21:46 -0700 (PDT) Received: from [192.168.0.82] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id a127sm3771532wmc.17.2016.04.29.08.21.45 for (version=TLSv1/SSLv3 cipher=OTHER); Fri, 29 Apr 2016 08:21:46 -0700 (PDT) To: internals@lists.php.net References: <3cc8a4c7-2640-11ae-a67b-06f909ac1e27@texthtml.net> <57173859.4080501@rochette.cc> <013101d19ff8$596b6010$0c422030$@tutteli.ch> Message-ID: Date: Fri, 29 Apr 2016 16:19:22 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------286534AA33A0C82EF9B8B88D" Subject: Re: [PHP-DEV] [RFC:generics] From: rowan.collins@gmail.com (Rowan Collins) --------------286534AA33A0C82EF9B8B88D Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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] --------------286534AA33A0C82EF9B8B88D--