Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101694 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 54215 invoked from network); 26 Jan 2018 14:20:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jan 2018 14:20:34 -0000 Authentication-Results: pb1.pair.com smtp.mail=tendoaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tendoaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.171 as permitted sender) X-PHP-List-Original-Sender: tendoaki@gmail.com X-Host-Fingerprint: 209.85.213.171 mail-yb0-f171.google.com Received: from [209.85.213.171] ([209.85.213.171:38508] helo=mail-yb0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6A/E2-35287-1393B6A5 for ; Fri, 26 Jan 2018 09:20:34 -0500 Received: by mail-yb0-f171.google.com with SMTP id l3so227914ybk.5 for ; Fri, 26 Jan 2018 06:20:33 -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 :cc; bh=Va70TfFzr7H0WtaVSPbNZH5DU3elbAe305oAwTRw2TA=; b=TEG9kuHkPm2/juoy1SCEd9bO6jA751Gw25YN7yMS+qpYGhcN0Mbc2xyalbKEQkBbv1 UXvWoCCtWu4678bg3JIntUj9JwtM22Q/0PmUsbOwNt+8svC/+phpo95IEx5dMSAkqQOz UEiebcQBIxccl/mytC5DUgpR5C5aZh952MahoXtfMTzlSMRu4MUXJ46qPNkLI4zunsCf c12qE9MRoSxW/GBIL7FbXG/ayZj3AuPcPT+6RjKSX9PW88b+kw79Bky7PGG+d+DRyg1w kbk6twTiaLOpvTyYV1ffx0TbEQiNPS/AvJu+M6YqVFNpdyt8+bNpPI6Av4CIQ3P6N/Wt e43g== 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:cc; bh=Va70TfFzr7H0WtaVSPbNZH5DU3elbAe305oAwTRw2TA=; b=pgF9QIKrjsTmIwrldm+7TX/J/ZxrAtLtqmooqupuikvejhnNXF8wgFBKXDI1AXwgEt KuAYDmD5IlevwZsiNLHQBCBeMOLB7BcUsU+IFkOUjk1EBQKaaPeRLVlFOFUuhAE85Nmw MH7zFXyR0bOyprkfQ+IlqVM3KXxeE958Ff8pHZuwk998T9LuofwiFfeV13GYja3Z788c irpo01T5md970jV6A4Lv+qygwJbm/yHZklUuaus1z2gXXtP3DiLjEPBO+m3YMZs+XKw0 5DwGSTK6yzGiCPP/wFBblTGMTDEVNHc5X6ZJFrRQl2HXkcD82Q5icFwm8UQQ1yDbod4A rk+w== X-Gm-Message-State: AKwxyteN2FNn0sf02Z4gNiu0A270C+Y3bw472Xf3ZmirNahqJTXBT3Jl SWe/k9SRIlbj/uGC+HodaD37yyjDBhjoFURqKjI= X-Google-Smtp-Source: AH8x224MEFdO78EJuJ3GvP3wip4jEn/7pd6KnZuWqwzW0lE2tI2fbsUnWLtJ2hnf03ND/lKFbGqrYO7sPJt+yMKUmjk= X-Received: by 10.37.173.75 with SMTP id l11mr11550057ybe.304.1516976431414; Fri, 26 Jan 2018 06:20:31 -0800 (PST) MIME-Version: 1.0 Received: by 10.37.189.16 with HTTP; Fri, 26 Jan 2018 06:20:31 -0800 (PST) In-Reply-To: References: <9b12b0e2-185e-86fb-0e0b-c4f24bfbd661@gmail.com> Date: Fri, 26 Jan 2018 08:20:31 -0600 Message-ID: To: Niklas Keller Cc: PHP Internals Content-Type: multipart/alternative; boundary="f403045ec2a053c1cb0563ae99e2" Subject: Re: [PHP-DEV][RFC][DISCUSSION] Collection Inspection From: tendoaki@gmail.com (Michael Morris) --f403045ec2a053c1cb0563ae99e2 Content-Type: text/plain; charset="UTF-8" On Thu, Jan 25, 2018 at 11:59 PM, Niklas Keller wrote: > Michael Morris schrieb am Fr., 26. Jan. 2018, 02:06: > >> On Thu, Jan 25, 2018 at 3:04 PM, Niklas Keller wrote: >> >> > >> >> >> >> $a instanceof array >> >> >> > >> > That might work, but array should only return true if it's an >> > array, not for anything that implements ArrayAccess. >> > >> > >> >> Related: >> >> On Thu, Jan 25, 2018 at 4:11 PM, Levi Morrison wrote: >> >> > >> > >> > I see no value to this operator being applied to non-array >> > traversables. >> >> >> If an array access object can't masquerade as an array it loses some of >> its >> value, but Niklas is right - it is important to distinguish such objects >> from native arrays. One solution would be to promote "iterable" to >> keyword >> status. The flexibility to take any iterable will be needed I think. >> >> $a instanceof iterable >> >> Would return true for anything iterable (which we can already test with >> is_iterable() ) where all values where strings. >> >> 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. > > > They can only define generator as return type, which is what they return > when you call a generator function. > > Even if you could declare the return type of the generator, you'd still > have the same problem with the yielded values. > See my comment about coding around morons being a pointless exercise. You throw an error and force the moron to fix their code. > > 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. >> >> >> >> > Returns true if $a is an array (or implements array access) and that all >> >> it's members are strings. >> >> >> >> $b instanceof SomeClass >> >> >> >> Returns true if SomeClass can be iterated and contains only strings. >> >> >> > >> > This would block generics with that syntax then. >> > >> >> I don't understand this comment. >> > > You restrict these type parameters to iterators, but generics are useful > in a lot more places. > iterABLE --- not iterOR. Two different things. Iterable is a psuedotype in PHP covering anything that can be traversed with foreach, including std_class, which is what I think what you mean by generator. There's even an existing function, is_iterable() to detect them. `$a instanceof iterable` will return the same as `is_iterable($a);` Generics specifically are already detectable by `$a instanceof stdClass` `$a instanceof Iterator` detected that interface, but not arrays and generics. `$a instanceof iterable` would detect all three. The similarity of the names is regrettable, but it's already in place and can't be changed at this point. --f403045ec2a053c1cb0563ae99e2--