Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92515 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75730 invoked from network); 20 Apr 2016 02:54:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Apr 2016 02:54:47 -0000 Authentication-Results: pb1.pair.com header.from=guilhermeblanco@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=guilhermeblanco@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.173 as permitted sender) X-PHP-List-Original-Sender: guilhermeblanco@gmail.com X-Host-Fingerprint: 209.85.223.173 mail-io0-f173.google.com Received: from [209.85.223.173] ([209.85.223.173:36227] helo=mail-io0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F4/20-22821-47FE6175 for ; Tue, 19 Apr 2016 22:54:44 -0400 Received: by mail-io0-f173.google.com with SMTP id u185so39256020iod.3 for ; Tue, 19 Apr 2016 19:54:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=elS1EFVuoRKUXHQLrLvybLmQcDAYz2By2Skbso5pcCk=; b=jG7ODBbeiR4U5cFYkjVA02PezmO0B2t0/V66NC8ccBWyRPfoc6oc2C3r/CoJsGUgj8 +Hxep+fRY8VAiKykIIx3L2Ok2C+9tKQXWn0esdG9Fev/pmXvWwRy0tHVYIFH2hcVOMej BipL9dl3h1iOqO4yaDtVlYTv6ICfQMmZvrXAWJ5cQ7GD5uI7/krb9FnDBuy23hILo5IA bFzDr40HrDxlj0Cb6m0sbv6j5bj7rAzpfXLL2NGfJ+QvUzSww/AlrKeVRG9Vh153q9nX PU076ToQToQ3p10tYIUeCtAH3KdgZ5iQgMVcqqG18WjOnON/6zGjY8UTc+Iesxy1gxD0 WZ5g== 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:from:date :message-id:subject:to:cc; bh=elS1EFVuoRKUXHQLrLvybLmQcDAYz2By2Skbso5pcCk=; b=WSaFXRvb0EE/VRTFq03sQkeemrRLKhrqUD+KbaL1PRFhtI0pnu51EIUVcOpCGyKw/i B1Oe6plN+FUykoTxo3QMe/3qnH+umhOm1LC+jzBxq0NX3bdpYG5pGjVG5pUjHc92X4j8 Jh8QA2HaeJ3TWB+3QY+f/ldK7tCcDpcMJ5t2QMarLpGgMKSy3YI5GXPQ+AB3ZSClgfYx K253IFZJdZe/4BdN0UD8s72A6kRKgvhClAjjrzKt45rL7+XFTN9rErHSxuvfDyz5vtHc y+mUrCgLlOvkpk6LYj5OjHU2U2JnbMAMIGXwS+IN9Usu9nlmRJUN/gxl2iAszVgqpojR GcZg== X-Gm-Message-State: AOPr4FWQjIosM4YeXhIp6GoNEL/TTw565TxQ4Qr3fTL3CzBco6PYG5It7fuNGTZewUtw0Z3o3lTG8NnuTLq6Ow== X-Received: by 10.107.62.136 with SMTP id l130mr7038722ioa.185.1461120881394; Tue, 19 Apr 2016 19:54:41 -0700 (PDT) MIME-Version: 1.0 Received: by 10.50.85.10 with HTTP; Tue, 19 Apr 2016 19:54:21 -0700 (PDT) In-Reply-To: References: <3cc8a4c7-2640-11ae-a67b-06f909ac1e27@texthtml.net> <5716BB89.8030005@gmail.com> Date: Tue, 19 Apr 2016 22:54:21 -0400 Message-ID: To: Sara Golemon Cc: Stanislav Malyshev , PHP internals Content-Type: multipart/alternative; boundary=94eb2c0883e01c08b30530e1b7b8 Subject: Re: [PHP-DEV] [RFC:generics] From: guilhermeblanco@gmail.com ("guilhermeblanco@gmail.com") --94eb2c0883e01c08b30530e1b7b8 Content-Type: text/plain; charset=UTF-8 Hi, Here are a couple of comments towards Generics support to PHP. 1- Even though mentioned, I'd still use "extends" or "implements" instead of "is" (which would be a new pseudo-reserved keyword) to enforce data type consistency and prevent developers to potentially referring to one thing while consider another. 2- I'd avoid blindly inferring generic type using constructor without notifying the user about this being a generic class. I'd still prefer a diamond operator <> to be used. This would be specially valid if we ever decide to merge typed properties too. Example: class Foo { private Bar bar; public function __construct() { $this->bar = new Bar<>("Hello World"); // Can be omitted because of typed properties already defined it } } 3- I didn't see any description related to nested generic type definition. I hope it is supported. Example: $pair = new Tuple>(); 4- Another thing not mentioned in the RFC is if constructor also support generic typing, like this: class Foo { public function __construct(B $b) {} } $foo = new Foo(1); 5- Java provides an enforcement that you can specify multiple bounds. Imagine a class that contains a generic that that must extend class A, but also implement interfaces B and C. We'd enter in a language recognition problem if we take the same approach as class declaration as definition, such as: class Foo In order to properly solve this, Java takes the approach of &, and require the class to be first and interfaces later, like this: class Foo. As I already mentioned in comment #1, we should stick to extends and implements, but adopt & as multiple interface implementation, like this: class Foo {} 6- This could be an independent RFC, but ideally we should support generic typed arrays using the [] syntax, like this: function sortElements(E[] $elements) : E[]; 7- Typed returns are also not mentioned in RFC: function newEntity($id) : E; $user = newEntity(UUID::v4()); 8- RFC only handles upper bound generic types, but no support is mentioned about lower bound generic types. An upper bound generic type is when it restricts the unknown type to that type or one of its inherited types. In RFC this is specified by the "is" keyword and I suggested to still segregate it to "extends" as highlighted in comment #1. A lower bound generic type is when it restrict the unknown type to that type or any of the is parent types. I'd suggest to use "parent", "parents", "parentof" or "super". Example: class A {} class B extends A {} class C extends A {} class D extends B {} class Foo {} // T can be an instance of A or B but never C or D. 9- Although not clearly clarified in RFC, I saw some inference over unbounded wildcard types. However, nothing is mentioned about wildcard upper bound and wildcard lower bounds. A few examples: // Unbounded wildcard type function sort(List $list) : void; // ... how Java (here, PHPized) does it: function sort(List list) : void; // Wildcard upper bound in Java (PHPized) function sortElements(List $list) : void; // Wildcard lower bound in Java (PHPized) function addAll(List $list) : void; There're problems of using the unbounded wildcard generic type, but I think you may have to dig through Java language specification to better understand what I'm saying. You can poke me privately and I can point out documentation that exposes its limitations/constraints. 10- Reflection plays a very important piece in this support and IMHO it must be documented with proposed API for verification. A link to a gist is not enough to fully understand what is proposed, so I'm abstaining from reviewing it until it gets properly mapped out in RFC. I hope I gave enough insight of what I consider that needs to be expanded in RFC, expanded in the proposal, highlighted better, etc. Still, this RFC is the best attempt I've seen towards improving the reusability of data structures in PHP. Nice work! =) Regards, On Tue, Apr 19, 2016 at 9:17 PM, Sara Golemon wrote: > On Tue, Apr 19, 2016 at 4:13 PM, Stanislav Malyshev > wrote: > >> class Collection {... > > > > I am sorry if this sounds harsh, but I really hope we won't have > > something like this in PHP. Java templates are complex and weird enough, > > adding another layer on top of that to allow type expressions IMHO is > > really taking it too far. > > > Nah, I've yet to actually have use for anything so specific yet > either, but in the context of Mathieu was asking for, it seemed like a > good moment to direct the concept back to something more consistent, > at least. > > -Sara > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Guilherme Blanco Lead Architect at E-Block --94eb2c0883e01c08b30530e1b7b8--