Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113129 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 56161 invoked from network); 10 Feb 2021 09:09:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Feb 2021 09:09:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8D6B41804DC for ; Wed, 10 Feb 2021 00:55:06 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) (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, 10 Feb 2021 00:55:05 -0800 (PST) Received: by mail-lf1-f47.google.com with SMTP id u25so1696375lfc.2 for ; Wed, 10 Feb 2021 00:55:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=z5r2f2kOY9nSLVGNn8XTUHw/i4FP1BuSLGCf0JxGptQ=; b=k8RGvq+Z6FP9Lv0SuSmI8+ZtyP0H6cLs3hB9YQsyvcDMxD52e5BFsol0LSQP26HyfC t4zBQucrY2KmMVBSms3aaNzrwmp2OcSMZPwvBPoq+vp1haPMDnjg/Bsg7aJzWVNSlfiY kAZ4jyzHixgXotH1VQ0HET+Z5gu6wKHq+PA6MIcHXoALxvpsjyqn4GEg0RKgNQmD0Zte RB5l3bnEVyMjIGn7r9T7NeOlENBp/CEJyNOT9nYSuHbsaargsnQzRdW6GZgpmnG7zbwj hrREJtWLzVkEajDlD1OW5A5ZSCfbeivlGMMy1/L5SnHfzK+4h2g67kPZzLb+E2yntSeb Hjyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=z5r2f2kOY9nSLVGNn8XTUHw/i4FP1BuSLGCf0JxGptQ=; b=e2cfP+qHC5eahlZ2OKeXXKqKg01Xfdlv7SxNH/pqk3eGeHWYBNZJAxvicwRpX1AC8q ipNYzkw6mRM0/kNWF32D1iXpQNdM4pUNg1B/ltyaP0r+QbA5hB7dHLTEeiuQHzKnx2p+ f1F+RdjZwulloNWDNQdfnY5J8TE0Sw2BlauTICXX7B/E5FXE+FpFSPNMw7EA/gUQKj9U RNg/G0Sm0/cGEK7BXPblrlqYsiOAZ8Rsw2dwbJc4dk7fSThPGdPvCHplGm04whrxXRyK rqDZzp7n5aTSYcO5xMd658rkKNgWSL79Kj86VEYo/HlY6m2uXJRRlUhgjROGj+2H+1GL 7+Xg== X-Gm-Message-State: AOAM533H93bSUWl1YsLLDo0vvx/E18SEzhIPCFfwdhE1ZfCReU11+n+U wvwCOeimSHQqKo2ODCOED4rUGOw3tmI64WwRyw== X-Google-Smtp-Source: ABdhPJywQEVuOQSAmZez089B235EvhIvZA/vrS8cV0QdKWwIzjxlvPmHg8WX08ZJE0o3R3sfvytNS/9I6N0IACdjbe4= X-Received: by 2002:a19:7ecf:: with SMTP id z198mr1125917lfc.650.1612947304301; Wed, 10 Feb 2021 00:55:04 -0800 (PST) MIME-Version: 1.0 References: <77dfe9e5-a6d1-4a41-bceb-454a65cf34d0@www.fastmail.com> <5ed1c099-6a0b-4e92-96a7-a1f21aefe753@www.fastmail.com> In-Reply-To: <5ed1c099-6a0b-4e92-96a7-a1f21aefe753@www.fastmail.com> Date: Wed, 10 Feb 2021 09:54:53 +0100 Message-ID: To: Larry Garfield Cc: php internals , tyson andre Content-Type: multipart/alternative; boundary="0000000000001d4a0105baf78fe7" Subject: Re: [PHP-DEV] [VOTE] PHP\iterable\any() and all() on iterables From: guilliam.xavier@gmail.com (Guilliam Xavier) --0000000000001d4a0105baf78fe7 Content-Type: text/plain; charset="UTF-8" Hi, On Tue, Feb 9, 2021 at 7:33 PM Larry Garfield wrote: > On Mon, Feb 8, 2021, at 6:48 PM, Levi Morrison via internals wrote: > > On Mon, Feb 8, 2021 at 5:15 PM tyson andre > wrote: > > > > > > Hi Larry Garfield, > > > > > > > > Hi Larry Garfield, > > > > > > > > > > > > Hi internals, > > > > > > > > > > > > > > Voting has started on > https://wiki.php.net/rfc/any_all_on_iterable and > > > > > > > ends on 2021-02-22. > > > > > > > > > > > > > > This RFC proposes to add the functions > `PHP\iterable\any(iterable > > > > > > > $input, ?callable $callback = null): bool` and > `PHP\iterable\all(...)` > > > > > > > to PHP's standard library's function set, using the namespace > preferred > > > > > > > in the previous straw poll. > > > > > > > > > > > > > > [...] > > > > > > > > > > > > > > > > > > Ak! I literally just finished reading it and wanted to note a > lack of clarity on one point. :-) > > > > > > > > > > > > The signature of the callback is never specified explicitly. > The ternary is a bit confusing. I assume the signature is > > > > > > > > > > > > callable(mixed): bool > > > > > > > > > > > > But that's not made explicit. It's also not made explict that > omitting the callable collapses to "is truthy". That's a sensible thing to > do, but it's not stated explicitly anywhere, just inferred from the code > sample. > > > > > > > > > > > > [...] > > > > > > > > > > If there is a callable, it allows `callable(mixed): mixed`, > > > > > and converts the callable's return value to a boolean. > > > > > So omitting the callable is the same as passing in the callable > `fn($x) > > > > > => $x`, which is equivalent to `fn($x) => (bool)$x`. > > > > > This is exactly what the reference implementation would do. > > > > > > > > > > [...] > > > > > > > > Oof. I'm glad I asked, because I don't like that at all. If > available, the callable should be returning bool, not "anything that may be > truthy/falsy." If you have an explicit function, it should have an > explicit return type. A truthy check is a reasonable default, but not for > when you're opting in to specifying the logic. > > > > > > > > I am in favor of the RFC, but I will have to consider if that > changes my vote to No. > > > > > > > > --Larry Garfield > > > > > > This was a deliberate choice and is consistent with the weak type > comparison behavior of array_filter() and other functions that default to > using weak type checks internally. > > > > > > I'd agree that I'd prefer to see callbacks returning booleans in code > I'm reviewing, > > > but a truthiness check seems more practical and consistent with the > rest of the language > > > than throwing a TypeError or checking the predicate return value using > `!== true` > > > > > > This was made to make PHP more widely accessible and free of surprises. > > > e.g. `(bool)array_filter($arr, $predicate)` can be safely converted to > `any($arr, $predicate)` without introducing a TypeError or behavior change. > > > > > > [...] > > > > For what it is worth, in C++ it is fairly normal to use a convertible > > to bool type. For instance, having an overload on a < b for iterators > > can return whatever type it wants, as long as it is contextually > > convertible to bool. > > Yet in 8.0, a non-[ 1 | 0 | -1 ] return from a comparison function as just > converted to a warning. So the trend in the language seems to be the other > direction. > Are you referring to this frequent mistake? ``` $list = [2,4,1,3]; usort($list, fn ($a, $b) => $a < $b); /* Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero */ echo json_encode($list); // [4,3,2,1] ``` (That's probably because PHP user comparison functions were modelled from C (e.g. strcmp()), not C++ (e.g. std::less).) Of course here the "correct" thing is to return `$a <=> $b` (or `$b <=> $a` for descending order), but you can also return `$a - $b` (not necessarily in [-1,0,1]), or even a string `"foo"` still without any warning in 8.0.2 (just a certainly wrong result)... Anyway, to me it feels natural that any()/all() would "work" like array_filter(). @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 add the optional `int $flags = 0` (or "$mode") parameter right from the start (even if not used yet), as adding it in a later release would apparently pose some BC concerns (ArgumentCountError, polyfills etc.)? -- Guilliam Xavier --0000000000001d4a0105baf78fe7--