Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116971 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 26096 invoked from network); 2 Feb 2022 13:45:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Feb 2022 13:45:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6ED99180381 for ; Wed, 2 Feb 2022 06:59:44 -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=-1.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_HOTMAIL_RCVD2, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8075 40.80.0.0/12 X-Spam-Virus: No X-Envelope-From: Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam08olkn2044.outbound.protection.outlook.com [40.92.46.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 2 Feb 2022 06:59:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SqcVssi0a5DQRCHRZHIdS4T+QtmBLGs4bu/DcFkBcDS6kxBpV6CT2UKFxeVFsqW5FN+68ukl8BmCt3NYd9zyoedhgF5rfnDDkwgq9Kkelm2VfcNhP/X/pN5hZrx5ggDU7riz3P8BFJbyBXTNEZLNR7pBOQeyjsq3/N+8apo/kVapgNDptk4jANZcAksUjwO3J4TGhnhBhUjAiLZHUpwa0LnuhEhQqzPGK3lRZuty230eZih1u1PRGDCz1awLTnMOa+io8/vKNqZnqWm+UbHcaApf80b0ylDaQ4mQK2yqnVa0K3zbF1UcEMwQHBIP5+hTAIoD1fsZZcIgZTsKNCeDow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=YjH7TEgtalJ5huTYMmq80HAf9giAqmm/tYoS75ZIITY=; b=lyr+5Oq4mMOhbsCMm1jCTPjHYtzeknaFXX+G1JS6b8KxMT3wfJMXUhqIxyJ27PLdDGqiYW5DbuHen4dLmJGLAF6gShpzMI7QrTeS3IgEmoFLz0MJrNf8Py8+4WMBUz9GLDZFTcda6BG4unJjQalHNE1/akftQyXIE4vSdRrO+8wBarLsSdHLJO9pVazG7GCxAENkS+zzJVmIFh57yZJiOmxmODCnyCEr7WvJzcgLnjiMpXbntzJzhNtkQHNpGvT8oIb+KeYKstITjLqf8dhLJQYXolUbo9Z75NDL8pvALPOtdyszD7wafzXUKMdUkUOfEvtkSw1C7mEKGM0o4lh/Rw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=YjH7TEgtalJ5huTYMmq80HAf9giAqmm/tYoS75ZIITY=; b=WS4/sWMDLMsO2d/KL3w7VFjlTc6BrI0n8ENYEE77sK5MDy+G0JcCLxoWTrjV8ZQIFSoLjfJ2QkwozicdcuRjeOBpDbyDyCpY7C2IKY92oQQY0dhIjzUs7Mnyo0c7WlpqsUsIFHPmEXYFlJ3LSjfpQfFjj1MMCM0h61IlW68TogF51/XGx7r9vHRZJG0NxKIM7+xRQDJniw5c+XN809kpug3gMpPgqPr1+JDYaqfM82zr+b/9DCUi7TFCRQLYjHZUEHGAwxkIFrFPYtCg0KtZpGyzo8qsO6Q2tPMJE6txnNJR7LJ/27aCdvMlPVQauyR6XMjj+1GIT7h3hh8vn4Ew2w== Received: from DM6PR14MB4155.namprd14.prod.outlook.com (2603:10b6:5:21e::11) by PH0PR14MB4439.namprd14.prod.outlook.com (2603:10b6:510:2d::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4951.11; Wed, 2 Feb 2022 14:59:42 +0000 Received: from DM6PR14MB4155.namprd14.prod.outlook.com ([fe80::1ddb:6eeb:a96d:9846]) by DM6PR14MB4155.namprd14.prod.outlook.com ([fe80::1ddb:6eeb:a96d:9846%4]) with mapi id 15.20.4930.022; Wed, 2 Feb 2022 14:59:42 +0000 To: php internals Thread-Topic: [PHP-DEV] Re: Adding `final class Deque` to PHP Thread-Index: AQHXraUxaK1dZ0niuECOJlLUpHNcpax/lZKygAAgIQCAAXQbEA== Date: Wed, 2 Feb 2022 14:59:42 +0000 Message-ID: References: <1c9e79d5-fd79-45a8-9c77-4228d770062d@www.fastmail.com> In-Reply-To: <1c9e79d5-fd79-45a8-9c77-4228d770062d@www.fastmail.com> Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: suggested_attachment_session_id: abb9e7c1-2a19-cad4-7bc3-31875bce650e x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [3Fb59LgP5se01hztpdo9yQW2lRNakENtKTxI/7kUpBPWL0UE2iYNSwILcMvwost5qo5AxektNyU=] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 231a19da-ae30-4f3f-fbf3-08d9e65ca490 x-ms-traffictypediagnostic: PH0PR14MB4439:EE_ x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: L97NgmyuS0ugKk0OHfl4kTora2WoJEq8LQybPG7JAJJ+MZVmGRX899zY4v6DYa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vieP37U942lUkpRwWhV/jFAEfDTir2+JHkNc9io7cz7?= =?iso-8859-1?Q?XxbFvp+5/ZEwZ5bd6pzKcfecQ8C/LgUkkkGo6D/mVl2NHYmdfnSujV/+bq?= =?iso-8859-1?Q?hzq6QY6f6aT5SOIxSbw12Gn9kZPeb80Az4cLk0vUkjzDiol1ruh33hdkp7?= =?iso-8859-1?Q?g8RoTCSR/U4VijlzleI+9exg+X+kssgzTaVt3GAJfTrltQuG+DGTTpvTj7?= =?iso-8859-1?Q?w/BrOB+ISzKoRrXguzWm+3Xi9zO3lN0MtHQ3y39+wv8k78mkOCmJyN0nAe?= =?iso-8859-1?Q?BUa3qu35Qlc/QUIDCpY9qOdZJWC08y3Fi+CfPjjYQ1ICNzhp3cAfMgRL8e?= =?iso-8859-1?Q?jHuCYCTaK0jZJ22hGLcIsTbE04ot2ywoAYLuDKOZaeCI+cV+kOFxrCbwmo?= =?iso-8859-1?Q?eh7t0BqzGTP/b9ZEff3rN3DZZsJ4GasyDTt9q9lg+ydQSPKFLw/zl+KD3a?= =?iso-8859-1?Q?PBeh36WuzDkbRjNPRmQY/XeR5Pjf681IB5oGfc2TNkapNtPcz2U+ybeP7P?= =?iso-8859-1?Q?V7eVKIRc9ukvXI6CV4szYi7FDAz4KzElcN4Zu/5FZR07ZGzAvvmslz+wMT?= =?iso-8859-1?Q?XiJmi+vMfP941lo1mp9OwrT/Tw3S8b9yfWSKxSoEsobSshKVFyxjM3gBCb?= =?iso-8859-1?Q?I7Tk1nsjyyPRBR3hzJZ4znP5oHaAJ+sGL6yj+Eo/Ma+v8aOrdUHWXLZm6R?= =?iso-8859-1?Q?pezIC7ASMOeH/TovQTzS+GmQGUGAVD6JVObZmpx+mcI6PKT3/TiQE+epSr?= =?iso-8859-1?Q?1g=3D=3D?= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-cd57b.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DM6PR14MB4155.namprd14.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 231a19da-ae30-4f3f-fbf3-08d9e65ca490 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Feb 2022 14:59:42.1356 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR14MB4439 Subject: Re: [PHP-DEV] Re: Adding `final class Deque` to PHP From: tysonandre775@hotmail.com (tyson andre) Hi Larry Garfield,=0A= =0A= > Request:=0A= >=0A= > push() and unshift() currently return void. That's not helpful. It woul= d be vastly more useful if they both returned $this. Not as much for chain= ing, but more so that you can add a value to a queue and pass it as an argu= ment to another call (often recursive, but not necessarily) in a single ope= ration.=0A= >=0A= > Example: I was doing last year's Advent of Code in functional PHP, and ha= d a stack walker that looked like this:=0A= >=0A= > function parse(string $line, $pos =3D 0, array $stack =3D []): Result|str= ing=0A= > {=0A= > $next =3D $line[$pos] ?? null;=0A= > $head =3D $stack[0] ?? null;=0A= >=0A= > return match ($next) {=0A= > // Opening brace, push an "expected" onto the stack.=0A= > '{' =3D> parse($line, $pos + 1, ['}', ...$stack]),=0A= > '<' =3D> parse($line, $pos + 1, ['>', ...$stack]),=0A= > '(' =3D> parse($line, $pos + 1, [')', ...$stack]),=0A= > '[' =3D> parse($line, $pos + 1, [']', ...$stack]),=0A= > '}', '>', ')', ']' =3D> $next =3D=3D=3D $head ? parse($line, $pos= + 1, array_slice($stack, 1)) : $next,=0A= > null =3D> count($stack) ? Result::Incomplete : Result::OK,=0A= > };=0A= > }=0A= >=0A= > The interesting part is the ['<', ...$stack], to pass on a modified versi= on of an array-as-stack. That's of course annoyingly slow with arrays righ= t now, and a Deque would be better, but only if it could be "modified and p= assed" like that. If not, it would be incompatible with single-expression = usages (match statements, short lambdas, etc.)=0A= >=0A= > Returning $this would resolve that, I think. (Making it return a new, im= mutable copy of the Deque would be even nicer, but I realize that's probabl= y not an argument I'm going to win at this point on this RFC.)=0A= =0A= Technically, you still can have single-expression usages in readable/unread= able ways=0A= =0A= - `[$deque->shift('value'), $deque][1]`, or=0A= - `($deque->shift('value') ?: $deque)`, or=0A= - `my_userland_helper_shift_and_return($deque, 'value')`=0A= =0A= My personal preference is against making this fluent.=0A= I'd rather expose an efficient datastructure that's consistent with the res= t of PHP's functionality to the extent possible,=0A= which userland can use to write their own fluent/non-fluent classes.=0A= There's drawbacks to returning `$this`, including:=0A= =0A= 1. Inconsistency with existing APIs making remembering what does what harde= r. Barely anything in php that I remember returns $this.=0A= =0A= https://www.php.net/manual/en/arrayobject.append.php returns void.=0A= =0A= https://www.php.net/manual/en/function.array-push returns an int.=0A= 2. Inconsistency with possible new datastructures/methods=0A= =0A= If a `Map`/`Set` function were to be added, then methods for add/remove = would return booleans (or the old value), not $this=0A= =0A= 3. Slight additional performance overhead for functionality I assume will b= e used relatively infrequently=0A= =0A= (php has to increment reference counts and opcache can't eliminate the o= pcode to decrease reference counts and possibly free the return value of `$= deque->shift()` with the return type info being an object)=0A= 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 wou= ld not immediately know what the code they're reading is doing.=0A= (less of a problem with a good IDE, typechecker, and a typed codebase, b= ut this isn't universal)=0A= Having it return void, `return $deque->push()` would be less common and = this would force the meaning to be clear.=0A= =0A= Developers might have several guesses/assumptions based on their experie= nce with other methods in php/elsewhere=0A= =0A= - It returns the new count (JavaScript Array.push, array_push)=0A= - It returns $this (Ruby)=0A= - It returns a lazy copy, like you'd wanted, not modifying the original= =0A= - It's returning void and the code in question is shorthand for `return = null`.=0A= (Python, C++ https://www.cplusplus.com/reference/vector/vector/push_ba= ck/ , offsetSet and spl push()/shift() methods)=0A= =0A= > Also, typo:=0A= >=0A= > "By introducing a data structure (Deque) that's even faster and more memo= ry usage than an array for use as a double-ended queue, even more applicati= ons would benefit from it. "=0A= >=0A= > I think you mean "less memory usage", or possibly "more memory efficient"= , or something like that.=0A= =0A= Thanks, I've fixed that.=0A= =0A= Thanks,=0A= Tyson=0A=