Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101686 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22253 invoked from network); 26 Jan 2018 05:59:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jan 2018 05:59:27 -0000 Authentication-Results: pb1.pair.com header.from=me@kelunik.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=me@kelunik.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kelunik.com from 81.169.146.218 cause and error) X-PHP-List-Original-Sender: me@kelunik.com X-Host-Fingerprint: 81.169.146.218 mo4-p00-ob.smtp.rzone.de Received: from [81.169.146.218] ([81.169.146.218:15782] helo=mo4-p00-ob.smtp.rzone.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 24/E1-04233-DB3CA6A5 for ; Fri, 26 Jan 2018 00:59:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1516946362; s=strato-dkim-0002; d=kelunik.com; h=Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:X-RZG-CLASS-ID:X-RZG-AUTH; bh=D8w9NXPDwTWDsme1rZ/dJtGWsHw+dZ2hprPENI6+/nY=; b=V2vnyp1FJdDs7mfi9+QUz/Q5Z6wXis0YoZJEuBnIoHyCRnc9WXhruaUAALUc6Wsmrl P5REFGsv9lO5J+FnudZ+wzi482easHSVsa+G5AnLnxtUW8TVfIxOVpkgtOMTsJoevItY e6diNDwPSegf4ZdJAVojyU+HtQD8Fnrkuz/7das0hieAUFLcUzmrpTQ3k5zT+7h0wRvS TbSQDv9KSJelQg8ibdqrEc/sQdZtS2JoZSPFJHvqff5Svwbb4hqaFc5WaFsRPDXWUEsB gCh0LROLz04dD06tQE5lhcoBHyxMlIocJ+KtxdGsglSpnEOMSHrNCF52f3+84/eZzjsT gNBA== X-RZG-AUTH: :IWkkfkWkbvHsXQGmRYmUo9mlsGbEv0XHBzMIJSS+jKTzde5mDb8AaBUcZi8tcA== X-RZG-CLASS-ID: mo00 Received: by mail-yb0-f181.google.com with SMTP id p83so3978412yba.4 for ; Thu, 25 Jan 2018 21:59:22 -0800 (PST) X-Gm-Message-State: AKwxytdDkniBM7V0TQutIzv/g3bifRvr2YcLYX0ExblrB5lifzr2svxh ZmHRzBKKSyerU1SoelaD9O0IM6KKvrrlFp4NPmk= X-Google-Smtp-Source: AH8x224gL4ZomYc4OlmpmWudLUyt4G/8UKslxSs9sMDCz2Ug5GSmMmpKXk3qTksos5elRP6Sfi/8ZVKQrEBckWoC+o0= X-Received: by 10.37.175.203 with SMTP id d11mr10276711ybj.501.1516946361854; Thu, 25 Jan 2018 21:59:21 -0800 (PST) MIME-Version: 1.0 References: <9b12b0e2-185e-86fb-0e0b-c4f24bfbd661@gmail.com> In-Reply-To: Date: Fri, 26 Jan 2018 05:59:11 +0000 X-Gmail-Original-Message-ID: Message-ID: To: Michael Morris Cc: PHP Internals Content-Type: multipart/alternative; boundary="089e082482780ab2cc0563a799bf" Subject: Re: [PHP-DEV][RFC][DISCUSSION] Collection Inspection From: me@kelunik.com (Niklas Keller) --089e082482780ab2cc0563a799bf Content-Type: text/plain; charset="UTF-8" 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. 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. > On Thu, Jan 25, 2018 at 5:28 PM, Larry Garfield > wrote: > > > > > > > Is this to ensure that everything coming OUT of a collection is a given > > type, > > or that everything put IN a collection is a given type? > > > > Ensure (or try to ensure, since reporting the promises of another method's > return type isn't certain) that things coming OUT are a given type. Leave > the headache of guarding inputs for another day and RFC. > > > > > > Asserting that "at this point in time, everything in this collection is a > > given type" is honestly fairly useless unless it's enforced to stay that > > way. > > > No more useless than type declarations are if a programmer does this... > > function foo (string $a) { > $a = (int) $a; > } > > Every feature of the language can be rendered useless by the right amount > of stupidity. No feature recommendation should be beholden to the "what if > a moron does X?" argument. > Regards, Niklas > --089e082482780ab2cc0563a799bf--