Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113131 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 81843 invoked from network); 10 Feb 2021 14:52:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Feb 2021 14:52:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 63F5E1804D0 for ; Wed, 10 Feb 2021 06:37:51 -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.9 required=5.0 tests=BAYES_00,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 NAM04-CO1-obe.outbound.protection.outlook.com (mail-oln040092010086.outbound.protection.outlook.com [40.92.10.86]) (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, 10 Feb 2021 06:37:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HvQqeIzLfpchleAGEDi9/PhsWx4u1TFsfro++UTeQiW/Vq774/6MlZ5BD8HKFyy3D0pn/otZ7feyUpVt+I7WXSMa6H4uIhqp6kdac9jUwrLndFn7p4XjTzaFcWJo4TiiD5Bqku6i5w29sOWYSCybWqnH0BSwCK3P3iIRS081e4TX9BAe80sC+qIJCe4vo+MZlRZQL6EK4RcmiDtA/s5pyyV0eqiOYnj63GmJnT5F4C7fDZncXw8FN8LSW9MjPWIUEuwAAQ9FWTvjUHYOQEcWlOhORh0V+dMu+N6s3MmDWkKPvXMXGP8do3FOUdKdXhQMQGRUG5ixbQvBkNYHeJ/cPQ== 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=WOi1EEVjtB9Qv01bgkzJjJzrhlZA3FBKOfW+LE61sD0=; b=TkO103pPXzq/xEypoHtSX6e0DMzhpS61TYXsrTnsW0HV403YFzsr7Xj6qWtJLzdiuxqnn1EF6L+VvwIYCMcf4DjN+TgQqgIrzveEMzzjJ7wdePaXjmJKFGbdH71U+WrxdDo6aK/thsc6it5lzC/xY3u5oVTv8BhDRLwhczXFqwKoELEKthubq/HXblFbERubSjU/uaFVYCzW4z8PAWC+Lo7Z+w9sqxH49S3PxUmlN6RMDUcRnbR8oNiQ4lxa8AQ/sonJYuri+/89pJk/wiP1FKf5crpvdHB72w/dw/nHVuv5lzbJOZ3Ar0qPQz27qOSvssuYKpPBKrZAXjqAVK8aMw== 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=WOi1EEVjtB9Qv01bgkzJjJzrhlZA3FBKOfW+LE61sD0=; b=ZlHZW+dMyEj3TT4Y5aA4OF60/USdE00tB5HCdB7ib0UFGcnSXs2o5pU2pmkZhOKm8CKkmObZ9M5GoLTtAxzoTUZA9DAirlKWE4dpes/+8BH5+krg39ObYDRS3vDAbTh4WF4qYaRsUs+A07sBoaRSLoKHcNp3mUeymCVxJwYN4tv/72l3oj+t2EzrtUxpgWXm8m9tpZ1Qu+9EEU9+hQnmdrcjXCrHaAAarZWsICOmzhLLD2lv7Aqr2ckgFUORUVcxt2yu6qq0n24aDSn83LCjmTIQSkBjEufuC3Br4A/c4QmV411uluG1/2/edF8OHpHmCR0Fd1MeimbK7Yq5uYv7Zw== Received: from CO1NAM04FT055.eop-NAM04.prod.protection.outlook.com (10.152.90.58) by CO1NAM04HT191.eop-NAM04.prod.protection.outlook.com (10.152.90.226) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.12; Wed, 10 Feb 2021 14:37:48 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com (2a01:111:e400:7e4d::50) by CO1NAM04FT055.mail.protection.outlook.com (2a01:111:e400:7e4d::273) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3846.25 via Frontend Transport; Wed, 10 Feb 2021 14:37:48 +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; Wed, 10 Feb 2021 14:37:48 +0000 To: Guilliam Xavier , Larry Garfield CC: PHP internals Thread-Topic: [PHP-DEV] [VOTE] PHP\iterable\any() and all() on iterables Thread-Index: AQHW/ibG3D0K4gIX3kCegHwWEoc5IqpOVSSAgAA2OhOAAExdgIAAGauXgAAM4ACAASllgIAA8NKAgABczC4= Date: Wed, 10 Feb 2021 14:37:48 +0000 Message-ID: References: <77dfe9e5-a6d1-4a41-bceb-454a65cf34d0@www.fastmail.com> <5ed1c099-6a0b-4e92-96a7-a1f21aefe753@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:AE7ABF286A50825136D1AC8032F8073E37A1699CE6B6996042D77E3FBC2918BC;UpperCasedChecksum:C854E2C3E4BA8E0BCE0819C6A993FCCF865686911B5E723F3E33B939EDCFA12E;SizeAsReceived:7675;Count:45 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [/kOCIJgX49WCt4CfnhzyVsbycdGJC+GzFBus2+MgG2SxOLQctxue6Psig1vHgPUn] x-ms-publictraffictype: Email x-incomingheadercount: 45 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 12548a09-6b4e-4293-db59-08d8cdd17032 x-ms-traffictypediagnostic: CO1NAM04HT191: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: P3bmcSh221DwT+AQH05ritJLY9EjggMKCrfqqkv5YLnoBDC4SqtnCNnowh+xTu1XdJM20yAXVkXuFMw4OIAgcw9zbQLtqzXdiKgoiQVSyzoxgftnhF6z5a8Mdrwym4CGP43Uj46B8x6TGBC4ptfcGZKKyDJf5Newegty522v9rddtVpssFRQQ9zzihpXKSGCXY5ohLhbs3EJ62328jgD7qlehsHnZAIicpSUeafjdSWTT/MmUyHec3paueotvd4JyHNPfgQXZYwHLiEOkwVdE1X3FAw/nPa5+fDOjcU+KIjlaGYpBY5LoqMjfWOFHg9e5enFih6JpoDxOMop2NdZQW5wrR7hns1fw7fKJm6bdziQeg8dD/qJXACsWBX7UugSEeSUzUslfjnI6vCa5BPlu+UaQGt8v4v5kOJdbnXBA9bwlT2bHb8EB3eBZqzuCro7 x-ms-exchange-antispam-messagedata: TnUyCtGcQN6mhjYdT+WbZd/oPC2a40sY1pLbPAH39XUoLBWUAsNhrpHlX2tjcJCRboNK/3kCSClO4AU8Z8IShOOruFJarnH7X6OVsS5IrlaCIRlaBeRtBEgmTcZLKWTfN0HWVzFQfYWkorHz8dA8vPHZ2vV4D448+QNIZ8OM9NXNES626bHu11vDpnmFOll05aI0HOWmD9gtsunqKi0Cbg== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: CO1NAM04FT055.eop-NAM04.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 12548a09-6b4e-4293-db59-08d8cdd17032 X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Feb 2021 14:37:48.6485 (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: CO1NAM04HT191 Subject: Re: [PHP-DEV] [VOTE] PHP\iterable\any() and all() on iterables From: tysonandre775@hotmail.com (tyson andre) Hi Guilliam Xavier,=0A= =0A= > > > 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_ite= rable and=0A= > > > > > > > ends on 2021-02-22.=0A= > > > > > > >=0A= > > > > > > > This RFC proposes to add the functions `PHP\iterable\any(iter= able=0A= > > > > > > > $input, ?callable $callback =3D null): bool` and `PHP\iterabl= e\all(...)`=0A= > > > > > > > to PHP's standard library's function set, using the namespace= preferred=0A= > > > > > > > in the previous straw poll.=0A= > > > > > > >=0A= > > > > > > > [...]=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 ternary 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 tha= t omitting the callable collapses to "is truthy".=A0 That's a sensible thin= g to do, but it's not stated explicitly anywhere, just inferred from the co= de sample.=0A= > > > > > >=0A= > > > > > >=A0 [...] =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= > > > > >=A0 [...] =0A= > > > >=0A= > > > > Oof.=A0 I'm glad I asked, because I don't like that at all.=A0 If a= vailable, the callable should be returning bool, not "anything that may be = truthy/falsy."=A0 If you have an explicit function, it should have an expli= cit return type.=A0 A truthy check is a reasonable default, but not for whe= n 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 chang= es my vote to No.=0A= > > > >=0A= > > > > --Larry Garfield=0A= > > >=0A= > > > This was a deliberate choice and is consistent with the weak type com= parison behavior of array_filter() and other functions that default to usin= g weak type checks internally.=0A= > > >=0A= > > > I'd agree that I'd prefer to see callbacks returning booleans in code= I'm reviewing,=0A= > > > but a truthiness check seems more practical and consistent with the r= est of the language=0A= > > > than throwing a TypeError or checking the predicate return value usin= g `!=3D=3D true`=0A= > > >=0A= > > > This was made to make PHP more widely accessible and free of surprise= s.=0A= > > > e.g. `(bool)array_filter($arr, $predicate)` can be safely converted t= o `any($arr, $predicate)` without introducing a TypeError or behavior chang= e.=0A= > > >=0A= > > >=A0 [...] =0A= > > =0A= > > For what it is worth, in C++ it is fairly normal to use a convertible= =0A= > > to bool type. For instance, having an overload on a < b for iterators= =0A= > > can return whatever type it wants, as long as it is contextually=0A= > > convertible to bool.=0A= > =0A= > Yet in 8.0, a non-[ 1 | 0 | -1 ] return from a comparison function as jus= t converted to a warning.=A0 So the trend in the language seems to be the o= ther direction.=0A= > =0A= > Are you referring to this frequent mistake?=0A= > =0A= > ```=0A= > $list =3D [2,4,1,3];=0A= > usort($list, fn ($a, $b) =3D> $a < $b); /* Deprecated: usort(): Returning= bool from comparison function is deprecated, return an integer less than, = equal to, or greater than zero */=0A= > echo json_encode($list); // [4,3,2,1]=0A= > ```=0A= > =0A= > (That's probably because PHP user comparison functions were modelled from= C (e.g. strcmp()), not C++ (e.g. std::less).)=0A= > =0A= > Of course here the "correct" thing is to return `$a <=3D> $b` (or `$b <= =3D> $a` for descending order), but you can also return `$a - $b` (not nece= ssarily in [-1,0,1]), or even a string `"foo"` still without any warning in= 8.0.2 (just a certainly wrong result)...=0A= =0A= A callback function called **once** (before php 8.0) on a pair mapping 2 st= ates (true, false) onto 3 states (-1, 0, 1) only worked coincidentally=0A= because the sort wasn't stable. The desired type had more possible values t= han the returned type, so it did the wrong thing.=0A= - This is different in that there's only 2 desired values.=0A= =0A= https://wiki.php.net/rfc/stable_sorting fixed that calling it twice and dep= recating booleans.=0A= It still allows you to return strings (not likely in practice) and casts th= ose to integers.=0A= =0A= "Second, if boolean false is returned, PHP will automatically call the comp= arison function again with arguments swapped.=0A= This allows us to distinguish whether the =93false=94 stood for =93equal=94= or =93smaller than=94. This fallback behavior should be removed in a futur= e version of PHP."=0A= =0A= > Anyway, to me it feels natural that any()/all() would "work" like array_f= ilter().=0A= > =0A= > @Tyson by the way, in the any()/all() case (vs the any_value()/all_values= () and potential any_key()/all_keys() etc.), wouldn't it be preferable to a= dd the optional `int $flags =3D 0` (or "$mode") parameter right from the st= art (even if not used yet), as adding it in a later release would apparentl= y pose some BC concerns (ArgumentCountError, polyfills etc.)?=0A= =0A= If an iterables RFC passed, and all amendments got approved before 8.1 stab= le was released, it wouldn't matter. There's months before the feature free= ze.=0A= =0A= If a function was added with flags "just in case" in 8.1 stable and we neve= r used the flags, we'd have to deprecate them and remove them in a subseque= nt major release.=0A= I don't think that's worth it.=0A= =0A= Cheers,=0A= Tyson Andre=0A=