Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101688 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 34158 invoked from network); 26 Jan 2018 09:56:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jan 2018 09:56:08 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.41 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.41 mail-wm0-f41.google.com Received: from [74.125.82.41] ([74.125.82.41:50771] helo=mail-wm0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2B/23-04233-83BFA6A5 for ; Fri, 26 Jan 2018 04:56:08 -0500 Received: by mail-wm0-f41.google.com with SMTP id f71so125630wmf.0 for ; Fri, 26 Jan 2018 01:56:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=NsPcO4LhMQdv309nARq9Uj7fA9h7rv2eB6yfTTQqPIw=; b=b/+YmdLhOWHgzBgslzYBpQlZams94MqLGNFwjTbksRmmRXMSBW0T/RDjyns9GZvOnP m6J67mUL5AzIWK4sJfeEjEi0x2tUMPYbnjZJsUJ+9aZhOAODKAn5cEeDegmlXNgwWkTS G7kqFKwotCAeCsuLBbJDbRZKWqsOEaW5dGE8ySF0JIXt+fQpkh88k+Q61cMuqR7UOohE 2DTiZDFB2h+r3paTkU5uOm9tY9NQzDGPs/waPNJeWfzQW4iDmN7unqXmsXYKSwibgQ/9 +3UnJ09IEP90fjWhOJexEqjK+Xic0dxtfoR1PQVcAOqUt1OzO8kobW7UbNVOMRaODk1D 2z+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=NsPcO4LhMQdv309nARq9Uj7fA9h7rv2eB6yfTTQqPIw=; b=X/8ZvYP8gb6r8c4386EoXJWcls6nris60MZ9gvPteVdf7yCcRWAwGDHCL6QPNlPnRM MXE27gElIgskq0IT20lswk88YV1y/qw5K6N1Dx5YMCMDX0m7VIXxXP7IvBQnpGCfa0XL qRZjW4KipSfRsESibEWFC7mi1MdAupySZ06qVJkTZjjJy1pi2e8tYgZjDCgbxjvWm6pK zukzrvEfoVH1HYq8V65hLi9h7X4VOYLE6DwhkGJMYvuVVJELa2VfE6A/nLIF9ue76yz1 qmra9lxYNDXns/f28yV5KFdm/PSdxVnGQoIQRPcXEZcmyCtdr6HMQeYNr8UjghhZFJeu IaRw== X-Gm-Message-State: AKwxytfWiZXus45UT+Tnf5mMaJpNpLytAq+cALu4+PoK8GytI+NVMNKF 3ruGCHMzfac2Ozg65v6gRuSGkmdagdO2T3ggag7YdQ== X-Google-Smtp-Source: AH8x224KVnmwBQRkcH9lh5TksEcYxsn2fBDbqyPeZ0sYMWH924BsvrcfrOU3bpDrvFQtaUeDfIZBO9ZbScKKM2qmK5g= X-Received: by 10.80.181.197 with SMTP id a63mr23775778ede.138.1516960565534; Fri, 26 Jan 2018 01:56:05 -0800 (PST) MIME-Version: 1.0 Received: by 10.80.222.195 with HTTP; Fri, 26 Jan 2018 01:56:04 -0800 (PST) In-Reply-To: References: <9b12b0e2-185e-86fb-0e0b-c4f24bfbd661@gmail.com> Date: Fri, 26 Jan 2018 09:56:04 +0000 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="94eb2c194adea5a5d60563aae7d6" Subject: Re: [PHP-DEV][RFC][DISCUSSION] Collection Inspection From: rowan.collins@gmail.com (Rowan Collins) --94eb2c194adea5a5d60563aae7d6 Content-Type: text/plain; charset="UTF-8" On 26 January 2018 at 01:06, Michael Morris wrote: > > On Thu, Jan 25, 2018 at 4:11 PM, Levi Morrison wrote: > > > > Our iterators cannot always be reliably rewound, such as > > when using generators. Checking that the generator returns only > > strings would consume all the input and would therefore be useless. > > > True - I hadn't thought of those. But as of PHP 7 generators can type > declare their return value. So, given `$a instanceof iterable`, if > $a is a reference to a generator, then the engine could check the return > type declaration and only give true on a match without attempting to use > the generator. > > We can follow this pattern farther - The return of an > ArrayAccess::offsetGet and Iterator::current() can be similarly tested by > instanceof rather than actually pulling data from these methods. > > We are having the return rely on the promise of the code, but in each case > TypeError would be raised anyway if it breaks it's promise to instanceof so > errors aren't being avoided. > The more angles we approach this, the more it looks like generics, or at least the same basis. For instance, what you're describing here is that Iterator would act like an extra interface that restricted the return type of current() to string. With full userland generics, that would actually be declarable like this: interface Iterator extends Iterator { public function current(): T; public function next(): T; } Which would basically be a template so that Iterator created an appropriately constrained interface, which you can actually create already: interface Iterator__string extends Iterator { public function current(): string; public function next(): string; } (You could actually use an auto-loader hack to do a lot of generics this way.) The main differences I can see between this and your suggestion are: - If it's an actual interface, the class's definition would need to explicitly list that it implements it. The wording you used implied that it might be more implicit, and automatically label the class as an "iterable" if the signatures matched. - The iterable syntax would be able to cover arrays as well as Iterators. We might decide that just as "iterable" stands for "Iterator or array", "iterable" stands for "Iterator or string[]". However, I think having "string[]" was previously rejected because of the cost of checking every element of the array, particular when the type is something slower to check, like "callable[]". I think this fits with where Derick was going earlier: we could have pseudo-generic interfaces like Iterator internally without a full generics implementation. As long as the syntax and semantics were compatible, these could then be a stepping-stone to full generics being added later. Regards, -- Rowan Collins [IMSoP] --94eb2c194adea5a5d60563aae7d6--