Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116973 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 32252 invoked from network); 2 Feb 2022 15:29:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Feb 2022 15:29:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B0C8B1804E3 for ; Wed, 2 Feb 2022 08:43:55 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 2 Feb 2022 08:43:55 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id E69523202305 for ; Wed, 2 Feb 2022 11:43:53 -0500 (EST) Received: from imap43 ([10.202.2.93]) by compute5.internal (MEProxy); Wed, 02 Feb 2022 11:43:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=zP7Bk8QjHb5/IVaNr h9GHWQ2NzXBw2BRNX3p+kZz16Q=; b=KTuzmW/gZDySdhIenyp2VIEc86Syp7bWz DPESl1x/gvenDWUNGB4L4Wobx6KGFLZAPRJ8xWTUj43M2B2RkzQRRkQnqW3CAA0A Mg5KMKyHM+ah2NnkP0Pnh9YGX6ZzRiJE3VPxQK9k0EOGf9LSLNyJaUFLGepJH4cQ Ywkm8NHMWnZFp7tPMTz81mzDMGzcH+p4lM3XvjnOpXfyMkiPV+svYh/IDM+Zp7iK AsyfJWWW8OHAWGGzRkySS64xzOhVXRdXyeKsxONIKCW1Ps5gjGXFnRIpSqOyUJfx pn5tg73LddWOof2iMOSoKLgBltVyrZEilF5rbqj4j6lil+qspGuAQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrgeehgdelvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgrrhhr hicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh eqnecuggftrfgrthhtvghrnhepudefuddvffdvudekueegkeelvdelvdelhffhkeethefh jeffteevkeevtdfgveegnecuffhomhgrihhnpehphhhprdhnvghtpdgtphhluhhsphhluh hsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhho mheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 09C61AC0E9A; Wed, 2 Feb 2022 11:43:53 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-4585-ga9d9773056-fm-20220113.001-ga9d97730 Mime-Version: 1.0 Message-ID: <3ae1a3f5-b4ea-40a0-b03f-3fa69ed6b9d2@www.fastmail.com> In-Reply-To: References: <1c9e79d5-fd79-45a8-9c77-4228d770062d@www.fastmail.com> Date: Wed, 02 Feb 2022 10:43:28 -0600 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Re: Adding `final class Deque` to PHP From: larry@garfieldtech.com ("Larry Garfield") On Wed, Feb 2, 2022, at 8:59 AM, tyson andre wrote: > Hi Larry Garfield, >> Returning $this would resolve that, I think. (Making it return a new, immutable copy of the Deque would be even nicer, but I realize that's probably not an argument I'm going to win at this point on this RFC.) > > Technically, you still can have single-expression usages in > readable/unreadable ways > > - `[$deque->shift('value'), $deque][1]`, or > - `($deque->shift('value') ?: $deque)`, or > - `my_userland_helper_shift_and_return($deque, 'value')` ... Those are all grotesque. > My personal preference is against making this fluent. > I'd rather expose an efficient datastructure that's consistent with the > rest of PHP's functionality to the extent possible, > which userland can use to write their own fluent/non-fluent classes. > There's drawbacks to returning `$this`, including: > > 1. Inconsistency with existing APIs making remembering what does what > harder. Barely anything in php that I remember returns $this. > > https://www.php.net/manual/en/arrayobject.append.php returns void. > > https://www.php.net/manual/en/function.array-push returns an int. So it's already inconsistent, and frankly neither of those returns is useful. If the existing convention is "inconsistently unhelpful", then let's go ahead and make it helpful since "it's lame but at least it's consistent" is not an argument here. (Sometimes that is a valid argument, but not in this case.) > 2. Inconsistency with possible new datastructures/methods > > If a `Map`/`Set` function were to be added, then methods for > add/remove would return booleans (or the old value), not $this Removal methods need to return the value being removed, sure. That makes sense across the board. But I don't see why Map::add($k, $v) or Set::add($v) shouldn't also return $this. I would make the exact same ask there, for the exact same reason: To aid functional code, which (when the syntax is conducive to it) can be more readable in many cases than procedural approaches. > 3. Slight additional performance overhead for functionality I assume > will be used relatively infrequently I would assume the opposite, at least in my own code. I'm writing a lot of recursive or reduction-based routines these days, so if I had reason to use a queue/stack in them in the first place, they'd almost certainly get used in this fashion. > (php has to increment reference counts and opcache can't eliminate > the opcode to decrease reference counts and possibly free the return > value of `$deque->shift()` with the return type info being an object) I cannot speak to this one; how much of a difference is it in practice? > 4. Returning $this makes code easier to write at some cost to > readability - Developers new to php or using `Collections\Deque` for > the first time would not immediately know what the code they're reading > is doing. > (less of a problem with a good IDE, typechecker, and a typed > codebase, but this isn't universal) > Having it return void, `return $deque->push()` would be less common > and this would force the meaning to be clear. I disagree that it's less readable. Compare it with the alternatives you showed at the start of your reply. None of those would qualify as "readable", IMO. > Developers might have several guesses/assumptions based on their > experience with other methods in php/elsewhere > > - It returns the new count (JavaScript Array.push, array_push) > - It returns $this (Ruby) > - It returns a lazy copy, like you'd wanted, not modifying the > original > - It's returning void and the code in question is shorthand for > `return null`. > (Python, C++ > https://www.cplusplus.com/reference/vector/vector/push_back/ , > offsetSet and spl push()/shift() methods) Once again, the lack of a consistent pattern means we don't need to follow the pattern. As with the first point, "it's silly but at least it's consistent" is a valid argument in many cases, and we've changed the language accordingly before. (Ternary associativity order comes to mind.) That's not the case here. There is no broad-based ingrained training that people have that would lead them to expect a given return, so we can and should go for whichever one is most powerful/flexible/conducive to good code. IMO, that's returning $this. (Again, since a purely functional version is apparently off the table.) --Larry Garfield