Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113120 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27218 invoked from network); 9 Feb 2021 00:30:34 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Feb 2021 00:30:34 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7BFD51804DC for ; Mon, 8 Feb 2021 16:15:21 -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=0.0 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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-Virus: No X-Envelope-From: Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10olkn2076.outbound.protection.outlook.com [40.92.42.76]) (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 ; Mon, 8 Feb 2021 16:15:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Sk7+UxcR+S/XJhk6l9hScjg17AAJ7NbWHgyDYTpHLo1EiNEwhungR8F9m9I77otoTaCh0q4QgfiquVGP7v1bVT6O72bJx88kIiWf8nl8TVL2jNanYW0HwjE/Pal/4TQV/uYV2Nuab53N/tgwGjbJHk8MC8gBP/Oe7k2zI47TnEvefAPvb5J9j7OginukKQBb+IaKTa8i+sMvxxGl5Vkc1n06lO8orALxJWXRJ1PSrTh7DIPBuPTX/puC2r2os/Xlw2ZKbdMBN4fdtLaFgwefWERpchKTL19IAnT5d0irNFgplRSwdF74Q2yYriZeG6DGz+FHyDYvjCKm4EihpkuFhQ== 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-SenderADCheck; bh=7G6+6y0xBMxEUC2FukP2i6ZCGQUvDZqRmVBJYMVvua8=; b=jXjoq9sZyYQgsP2fgadBM08s/LNZPg9iWO3sqzQ9kU1pXhMKNKmw16e0dLSYUDPOCuM5HoBOHPUaVoMIRUsRiOYelSeXAaSjafoPSoPIjE4CDsDWYyXiaSh5pEmsLsWNllpy70RVGg1rLxl6IgrDKEVxTI/SrYACOnBbAakVyIppEdzfhqEhMFrvouRyx+nUwhbqLnD0ykE2ZyEgM/iWJzH5mb+YgrBta/UqB9Wvlex0Ef1qYcZCjCna2pEJLl1hA08yBeIVd74flyYvnG67yFrROP1/+cjnNmdP+nOr4myrv+qsH/36+2hdht4m4RboIZq5dwCGhr+hfpT1eHVrbQ== 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=7G6+6y0xBMxEUC2FukP2i6ZCGQUvDZqRmVBJYMVvua8=; b=QlEK2a+nRfLWFJ2YsMDexsnpnEJjKPlEIev7GhMIEHhfDpQzK34AVhGdLo5SOOx1ciHdOUYG+uGQVg5CTOJqQrpbaVIy+8hZdPfRwysWCJSH1S/7kWq84ZKIuGInySRhU3eeI7NteNJ/Gml28WA0l8skNW706wDxc9KzaWnTlRNejPn0zkKBV8cQJDQL91ziV9pQXjd5LxE0vR3WLlBLQSfQcwwiu2bWJ2fcRDp58sjr7JCm9q1ix2c2+/ds7k5uFbQvAqqNdoHMTXKmuHhc++/EqmabiPnKDul8BfwmNjZvjP/SUvbzEjy/pEEykO8CSzNwJrirgegfUw/kGNEs6w== Received: from MW2NAM10FT011.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e87::45) by MW2NAM10HT163.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e87::108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.11; Tue, 9 Feb 2021 00:15:18 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com (2a01:111:e400:7e87::45) by MW2NAM10FT011.mail.protection.outlook.com (2a01:111:e400:7e87::237) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.11 via Frontend Transport; Tue, 9 Feb 2021 00:15:18 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::b4c4:dc11:5337:821d]) by DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::b4c4:dc11:5337:821d%4]) with mapi id 15.20.3825.030; Tue, 9 Feb 2021 00:15:18 +0000 To: PHP internals Thread-Topic: [PHP-DEV] [VOTE] PHP\iterable\any() and all() on iterables Thread-Index: AQHW/ibG3D0K4gIX3kCegHwWEoc5IqpOVSSAgAA2OhOAAExdgIAAGauX Date: Tue, 9 Feb 2021 00:15:18 +0000 Message-ID: References: <77dfe9e5-a6d1-4a41-bceb-454a65cf34d0@www.fastmail.com> , In-Reply-To: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:FF0EF40425384B69CD1CF9B6819A176BA546258441EF0AAFC742E519D2E021EB;UpperCasedChecksum:3B7E5AAF1DAB97B13C9C9E1C36F39AFC62D2D0526BFD28F1871FF9086603C9AF;SizeAsReceived:7267;Count:44 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [w6dWqAlvnQTvUDKPJcjPbMZVEJ+FNeiyha+awQWKfm0OqvMVdd9xfMKTOTnNYmyV] x-ms-publictraffictype: Email x-incomingheadercount: 44 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 6ea6f057-17a5-4424-24fa-08d8cc8fc868 x-ms-traffictypediagnostic: MW2NAM10HT163: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: pbTNYd4x4STwxnY6OPLOqXS1ITh7NmMLKfYIVcgoBQBt/j7AdlRbnGBconow4pF+gNpaug7vstz2O8Mmz9D5tF2OCHCwY7BefSDRPILsAv3SQ5wkf2Wyfv35Ao8KjKFAGozBaQOKQMV0wpTemRSqH+WnXo+5KCofkOVEkfo7+HiIkYnELktNuQYr80oA75wvdpq4SyI2t7RCP3qBULouvDgnhe31ssN1CWggFZFj6UbeEm/q7hgUqDoEMD+gcfLoGH/F/SLev19BQN5EWw1QoLTSjBWtTLLl/5WUu4cfG6cQUJT/j4yqSQrj+mlSgtc/zE7fEwhDYFx7DlnEyxCvFja/VCaXLHhzYyEOJ6HuMvlUxrs6x+4iJuPxxHsHhgkZyq++SwEhgz1Knch1Lfb8NBvnKGhN/esfBytBLduTaSz77zr25Fl8+ORuFYCdmzxp x-ms-exchange-antispam-messagedata: 4SIi92pdzOCz0YuGRztMurfedZS3QxdQ4MMxF8t/utq1ROXKhhQHN+a1Ag0CBNZT2DRoL43wZf7D095Fogeh298g+qDS2aOvQ8HcATjchRi3QEqpc4tXkHM8fEf4pCWjl4uGgvKQ3Wds5CqM9I8H9enhyyvjOz7uYN7UZ5wpVFyjYA+9W/dfW7MdH23JzAuV/tBmHmcw741A9gGQAjbj9A== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: MW2NAM10FT011.eop-nam10.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 6ea6f057-17a5-4424-24fa-08d8cc8fc868 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Feb 2021 00:15:18.6174 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet 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: MW2NAM10HT163 Subject: Re: [PHP-DEV] [VOTE] PHP\iterable\any() and all() on iterables From: tysonandre775@hotmail.com (tyson andre) Hi Larry Garfield,=0A= =0A= > > Hi Larry Garfield,=0A= > > =0A= > > > > Hi internals,=0A= > > > > =0A= > > > > Voting has started on https://wiki.php.net/rfc/any_all_on_iterable = and =0A= > > > > ends on 2021-02-22.=0A= > > > > =0A= > > > > This RFC proposes to add the functions `PHP\iterable\any(iterable = =0A= > > > > $input, ?callable $callback =3D null): bool` and `PHP\iterable\all(= ...)`=0A= > > > > to PHP's standard library's function set, using the namespace prefe= rred =0A= > > > > in the previous straw poll.=0A= > > > > =0A= > > > > There is a primary vote on whether to add the functions, and a =0A= > > > > secondary vote on the name to use within the `PHP\iterable` namespa= ce.=0A= > > > > =0A= > > > > Thanks,=0A= > > > > - Tyson=0A= > > > > --=0A= > > > > PHP Internals - PHP Runtime Development Mailing List=0A= > > > > To unsubscribe, visit: https://www.php.net/unsub.php=0A= > > > =0A= > > > =0A= > > > Ak!=A0 I literally just finished reading it and wanted to note a lack= of clarity on one point. :-)=0A= > > > =0A= > > > The signature of the callback is never specified explicitly.=A0 The t= ernary is a bit confusing.=A0 I assume the signature is =0A= > > > =0A= > > > callable(mixed): bool=0A= > > > =0A= > > > But that's not made explicit.=A0 It's also not made explict that omit= ting the callable collapses to "is truthy".=A0 That's a sensible thing to d= o, but it's not stated explicitly anywhere, just inferred from the code sam= ple.=0A= > > > =0A= > > > I'm not sure if it's safe to clarify at this point as the vote just s= tarted.=0A= > > =0A= > > If there is a callable, it allows `callable(mixed): mixed`,=0A= > > and converts the callable's return value to a boolean.=0A= > > So omitting the callable is the same as passing in the callable `fn($x)= =0A= > > =3D> $x`, which is equivalent to `fn($x) =3D> (bool)$x`.=0A= > > This is exactly what the reference implementation would do.=0A= > > =0A= > > I definitely should have clarified it instead of assuming that the =0A= > > reference implementation was clear enough.=0A= > > =0A= > > I clarified this and gave examples because the RFC started a few hours = =0A= > > ago and the implementation didn't change.=0A= > =0A= > Oof.=A0 I'm glad I asked, because I don't like that at all.=A0 If availab= le, the callable should be returning bool, not "anything that may be truthy= /falsy."=A0 If you have an explicit function, it should have an explicit re= turn type.=A0 A truthy check is a reasonable default, but not for when you'= re opting in to specifying the logic.=0A= > =0A= > I am in favor of the RFC, but I will have to consider if that changes my = vote to No.=0A= > =0A= > --Larry Garfield=0A= =0A= This was a deliberate choice and is consistent with the weak type compariso= n behavior of array_filter() and other functions that default to using weak= type checks internally.=0A= =0A= I'd agree that I'd prefer to see callbacks returning booleans in code I'm r= eviewing,=0A= but a truthiness check seems more practical and consistent with the rest of= the language=0A= than throwing a TypeError or checking the predicate return value using `!= =3D=3D true`=0A= =0A= This was made to make PHP more widely accessible and free of surprises.=0A= e.g. `(bool)array_filter($arr, $predicate)` can be safely converted to `any= ($arr, $predicate)` without introducing a TypeError or behavior change.=0A= =0A= ```=0A= php > var_dump(array_filter([-1,0,1], fn($x)=3D>$x));=0A= array(2) {=0A= [0]=3D>=0A= int(-1)=0A= [2]=3D>=0A= int(1)=0A= }=0A= ```=0A= =0A= This is the same choice as many other dynamic languages that aren't compile= d ahead of time have made.=0A= =0A= ```=0A= # python=0A= >>> any([1])=0A= True=0A= >>> any([0])=0A= False=0A= # Ruby=0A= irb(main):001:0> [nil].any?=0A= =3D> false=0A= irb(main):002:0> [false].any?=0A= =3D> false=0A= irb(main):003:0> !!0=0A= =3D> true=0A= irb(main):004:0> [0].any?=0A= =3D> true=0A= # JavaScript=0A= > [0].some(x=3D>x)=0A= false=0A= > [1].some(x=3D>x)=0A= true=0A= ```=0A= =0A= It is currently possible to check if code is passing a callable returning a= nything other than a boolean=0A= to functions such as `array_filter()` using a wide variety of static analyz= ers/tools, e.g. http://github.com/phan/phan=0A= =0A= ```=0A= $x % 3);=0A= ```=0A= =0A= Thanks,=0A= -Tyson=0A=