Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96647 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15543 invoked from network); 30 Oct 2016 12:47:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Oct 2016 12:47:27 -0000 Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.86 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.86 mx105.easyname.com Received: from [77.244.243.86] ([77.244.243.86:34751] helo=mx207.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 24/70-25911-BDBE5185 for ; Sun, 30 Oct 2016 07:47:26 -0500 Received: from cable-81-173-132-21.netcologne.de ([81.173.132.21] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1c0pWC-0005LV-FF; Sun, 30 Oct 2016 12:47:20 +0000 Reply-To: internals@lists.php.net References: <44244cfd-c8ad-e0a9-4e70-380a8a8c940c@syberisle.net> <01f2f3da-592b-fd23-709d-ad620422f56d@fleshgrinder.com> To: Rasmus Schultz , PHP internals Cc: Nikita Popov , Levi Morrison , David Lundgren Message-ID: Date: Sun, 30 Oct 2016 13:47:07 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Allow Iterator to be used with current, next, reset, key functions From: php@fleshgrinder.com (Fleshgrinder) On 10/30/2016 1:31 PM, Rasmus Schultz wrote: > On second thought, I agree with that - changing reset() and end() > doesn't make sense, because people know them and expect them to work > in a certain way. Likely a lot of people would actually continue to > use them with intermediary variables the way they do today. Better > to introduce a new pair of functions, since this will make it clear > when consumer code depends on the new behavior - if we update the > existing functions, that means you have to read code knowing which > version of these very common (and very old) functions you were > expecting to call. > > One thing though, since we have to introduce new functions, I would > not suggest these be array_first() and array_last(), but rather > simply first() and last(), and make then work with anything iterable, > not just arrays. > `first()` and `last()` are extremely generic names and I am still hoping to see nikic's scalar objects extension to land in core before 8. In this case one would always call `$x->first()` and `$x->last()`. Continuing with the `array_` prefix makes perfect sense to me, even if they accept `\Traversable` instances as well. We also expect `str_` prefixed functions to accept stringable objects, dont' we. It's just a prefix for grouping and not necessarily a restrictions regarding the types the function accepts. On 10/30/2016 1:31 PM, Rasmus Schultz wrote: >> Retrieving the first and last would mean to iterate all the >> results. > > Well, retrieving the last would - retrieving the first would mean > iterating only over the first result. For a lot of use-cases, and > unbuffered database results in particular, this is precisely what > you'd want. > >> An additional interface should be required for traversables in >> order to work with first and last. > > I think it would be great to have that as an option - for cases > where you can and want to optimize retrieval of the last item, but I > don't think it should be required? For example, in the case of an > unbuffered database query, there is likely no optimization that can > be made for last() in the first place, since the database > client/server are likely using a protocol that doesn't even allow you > to skip to the last result; in that case, requiring everyone to > implement a new interface, which does nothing, isn't meaningful. > Not requiring the interface means that we violate the Liskow's substitution principle, something I see too often in core and successful PHP software out there. What if a traversable does not want the first and last to be retrieved in this manner? Throw an exception? This violates the principle since it is not expected to behave in this way. Another possibility would it be to return null but then the question is, was null returned because the first or last element is null or because I cannot retrieve the first and last. Making it explicit is much better. -- Richard "Fleshgrinder" Fussenegger