Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96646 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 13412 invoked from network); 30 Oct 2016 12:31:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Oct 2016 12:31:38 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.217.179 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.217.179 mail-ua0-f179.google.com Received: from [209.85.217.179] ([209.85.217.179:36116] helo=mail-ua0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1D/00-25911-928E5185 for ; Sun, 30 Oct 2016 07:31:37 -0500 Received: by mail-ua0-f179.google.com with SMTP id b35so14670528uaa.3 for ; Sun, 30 Oct 2016 05:31:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=20H2qWmvHBlr6Xv3agHLRKPFovYVYUJAuFnXe7tWu3w=; b=HESTrWWC8RuMYYrGWVKL6G3MzUd7Fga4tQ+rFbOPXd5SwFIHvaBEU78V5GXCqsYmsS EVSaLFl7KSpUofu8x4KxEjBCSuAeoA2bd3X3d77cBKiL5gJZKq09vm7In+ajdrXSuYIB s2MqIjT04H4FDcWXvNwQt5/lh3ESSZtHztkpKWqN2mIkBne08Hzc0w/hPn2ADZpGRNC6 +vgqIbU+AHbf1IqjaJeHf5SafQkoEnVbHtdGltbnbd6rz+w3T7sWulXKKCg4XnSVGZlQ Vt2DlKlswCKe24RXmQgsTRRIA2TSCqR2iXfcZ9gbQJspsLr2XK8/IqdzffGJBgOX3hrr /Crw== 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=20H2qWmvHBlr6Xv3agHLRKPFovYVYUJAuFnXe7tWu3w=; b=DSmO84BClLJv65+106BMw/D/ixk5cDNyltCYTxZlzPnmm1OptmpI3saEmAcnaqtPl1 nxLVKoVAguX0vtMiwxSkOAatc7F2rDyw7IqOPdE/oLCWKPXcCpOKv84Oeg5awQKup1FA JfX2bBynSMDuJY9q3+BTu+1ADoTSFCwMUNoX1BobN29UNdgK3lEfbbbPB/g4wygLvxbM PBYgT3JZrTOGNhFn1gWKbeXuYOTNj20N1799hCQiSunSPCpVGGlG3R8xfTo70llJrC8b QSYkkPtagBGPhdIgptPupqWWQG4FWz0qZZJEQVfaVIgAqYQHZwBZ6SQKdX1kXYopPbVn UIUA== X-Gm-Message-State: ABUngvd74ZWbVHSyaqfd+MuAjG+RhZdVkeKTug0Z/WxsfjEGJtRXWB8Glh8IkP6N4w/1O4KBxFsw3wqeZOBA4Q== X-Received: by 10.159.51.9 with SMTP id o9mr19193023uab.155.1477830694998; Sun, 30 Oct 2016 05:31:34 -0700 (PDT) MIME-Version: 1.0 Received: by 10.103.99.4 with HTTP; Sun, 30 Oct 2016 05:31:34 -0700 (PDT) In-Reply-To: <01f2f3da-592b-fd23-709d-ad620422f56d@fleshgrinder.com> References: <44244cfd-c8ad-e0a9-4e70-380a8a8c940c@syberisle.net> <01f2f3da-592b-fd23-709d-ad620422f56d@fleshgrinder.com> Date: Sun, 30 Oct 2016 13:31:34 +0100 Message-ID: To: PHP internals Cc: Nikita Popov , Levi Morrison , David Lundgren Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Allow Iterator to be used with current, next, reset, key functions From: rasmus@mindplay.dk (Rasmus Schultz) 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. > 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. On Sun, Oct 30, 2016 at 1:07 PM, Fleshgrinder wrote: > On 10/30/2016 12:45 PM, Nikita Popov wrote: >> On Sun, Oct 30, 2016 at 12:24 PM, Rasmus Schultz wrote: >> >>> With regards to deprecating array-functions that operate on the >>> internal pointer - I'd like to just remind you of the fact that some >>> of these functions, reset() and end() in particular, are often used >>> not because someone cares about moving the internal pointer, but as a >>> means of getting the first/last items of an array. >>> >>> If the internal pointers are removed, I'd like to suggest we improve >>> the reset() and end() functions rather than removing them - there is >>> an annoyance with these two functions presently: because they operate >>> on the internal pointer, the force you to introduce often meaningless >>> intermediary variables. >>> >>> That is, for example: >>> >>> $last = end(explode(",", $comma_separated_values)); >>> >>> Doesn't work. One is forced to introduce a variable: >>> >>> $parts = explode(",", $comma_separated_values); >>> $last = end($parts); >>> >>> If internal pointers are removed, maybe the reset() and end() >>> functions could be made to actually work consistently? Whether applied >>> to an expression or variable. >>> >> >> Yeah, I've been thinking about this as well. reset() and end() do seem to >> be the most commonly used IAP functions -- while nobody actually cares >> about their effect on the IAP. The thing is, if we remove all other >> functions that allow users to inspect the IAP, then we can simply change >> reset() and end() to return the first/last element. As they would no longer >> actually modify the array, they would no longer accept their argument by >> reference, so end(explode(...)) would also become legal (and additionally, >> they would no longer have to do a full copy of their argument, like they >> currently sometimes need to do). >> >> Nikita >> > > I think it makes much more sense to deprecate all of them and introduce > new ones that work in the way we want them to for several reasons. > `end()` and `reset()` would have to continue working as they do until > the next major release since people who use them might rely on the way > they work. This means in effect that nobody can make her PHP 7 code PHP > 8 ready up front. > > Instead we could directly introduce `array_first()` and `array_last()` > with the next feature release -- even if the others are not deprecated > -- and everyone can update their code or at least use it in new code. > > Changing `end()` and `reset()` in PHP 7 is imho way to dangerous! > > Another often asked/proposed question is whether they should accept > traversables. I think especially for these two functions it's not easy > to simply accept any kind of traversable. Think of an unbuffered > database result which is effectively a remote pointer. Retrieving the > first and last would mean to iterate all the results. > > An additional interface should be required for traversables in order to > work with first and last. > > -- > Richard "Fleshgrinder" Fussenegger