Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113122 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31767 invoked from network); 9 Feb 2021 01:03:53 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Feb 2021 01:03:53 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EB0731804DC for ; Mon, 8 Feb 2021 16:48: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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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-io1-f47.google.com (mail-io1-f47.google.com [209.85.166.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 ; Mon, 8 Feb 2021 16:48:44 -0800 (PST) Received: by mail-io1-f47.google.com with SMTP id m17so6514719ioy.4 for ; Mon, 08 Feb 2021 16:48:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datadoghq.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=I9hVveC9eqP87WrvLWvcD9sqqO1WggpkmmjiJVTVzqw=; b=CRvqQwf8zXedSPx4ntod6KxltdP9HYgYfW1YuvNQk3HzsE9lz/kK5CYzKmhHv6+8JV bXygFrac1fUgFhBGA0c3shedJwTK0NpyysIylj2GUCM9EOYCfBxSqj1xRopt5iQFkJ47 HRPR96yYI2fcR19iwh6+LbbKA/DS7aoZwP+60= 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:content-transfer-encoding; bh=I9hVveC9eqP87WrvLWvcD9sqqO1WggpkmmjiJVTVzqw=; b=t6/Gipse5QUjM8aZW9epitjMmuu815SmyiJozLVEHEwLRwDOO/ZzqgEIflQUfFAtuy 7Rs5aWCq4TgZDjFM5/f3Zkny1n9S1SSCDtRCd3XCSOr+1d+kZ89RqMQ9iV/4nK4Y7zdv rku+foqHzs/bOLSfe9UhxESVw+uIljxb65q6o7F4z9VNdRgCoUh9SXXdqyLAlnbqc+oX Q693J4Mb/NFN8KY6OMMKRWPx0TTNqXdhlTmkCZnm5QIA/T74u6fZo1NxE7KFjIu5s07u sk5nn2VYNTQELBpnxbdZjR6YSFjgegy0BAcc8dEbd2qzEQn+E1ta6GhhpnKmsk+MGfuF Gw2g== X-Gm-Message-State: AOAM531KEbg2YChdJfDl+tPq68vx9KYHLmUwdmepU8IMWd6Z0PhyhCuE 97DRWJVx8xrOGuhReP3N43rzmzS657iTRrXh2rXlHQ== X-Google-Smtp-Source: ABdhPJxf4cSSzuaeFt4mzh8tVhL4o3cDTUgPk0NMFQLCD/dx6ZKJIJEdCAJ7EK8VEFyNARnLQyQNk04u6JoUY8q+LuM= X-Received: by 2002:a5d:9bc5:: with SMTP id d5mr17456143ion.119.1612831723410; Mon, 08 Feb 2021 16:48:43 -0800 (PST) MIME-Version: 1.0 References: <77dfe9e5-a6d1-4a41-bceb-454a65cf34d0@www.fastmail.com> In-Reply-To: Reply-To: Levi Morrison Date: Mon, 8 Feb 2021 17:48:32 -0700 Message-ID: To: tyson andre Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [VOTE] PHP\iterable\any() and all() on iterables From: internals@lists.php.net ("Levi Morrison via internals") On Mon, Feb 8, 2021 at 5:15 PM tyson andre wrot= e: > > Hi Larry Garfield, > > > > Hi Larry Garfield, > > > > > > > > Hi internals, > > > > > > > > > > Voting has started on https://wiki.php.net/rfc/any_all_on_iterabl= e and > > > > > ends on 2021-02-22. > > > > > > > > > > This RFC proposes to add the functions `PHP\iterable\any(iterable > > > > > $input, ?callable $callback =3D null): bool` and `PHP\iterable\al= l(...)` > > > > > to PHP's standard library's function set, using the namespace pre= ferred > > > > > in the previous straw poll. > > > > > > > > > > There is a primary vote on whether to add the functions, and a > > > > > secondary vote on the name to use within the `PHP\iterable` names= pace. > > > > > > > > > > Thanks, > > > > > - Tyson > > > > > -- > > > > > PHP Internals - PHP Runtime Development Mailing List > > > > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > > > > > > > > > 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 t= ernary 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 omit= ting 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 sampl= e. > > > > > > > > I'm not sure if it's safe to clarify at this point as the vote just= started. > > > > > > 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) > > > =3D> $x`, which is equivalent to `fn($x) =3D> (bool)$x`. > > > This is exactly what the reference implementation would do. > > > > > > I definitely should have clarified it instead of assuming that the > > > reference implementation was clear enough. > > > > > > I clarified this and gave examples because the RFC started a few hour= s > > > ago and the implementation didn't change. > > > > 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/f= alsy." 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 opt= ing in to specifying the logic. > > > > I am in favor of the RFC, but I will have to consider if that changes m= y vote to No. > > > > --Larry Garfield > > This was a deliberate choice and is consistent with the weak type compari= son behavior of array_filter() and other functions that default to using we= ak 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 `!= =3D=3D 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 `a= ny($arr, $predicate)` without introducing a TypeError or behavior change. > > ``` > php > var_dump(array_filter([-1,0,1], fn($x)=3D>$x)); > array(2) { > [0]=3D> > int(-1) > [2]=3D> > int(1) > } > ``` > > This is the same choice as many other dynamic languages that aren't compi= led ahead of time have made. > > ``` > # python > >>> any([1]) > True > >>> any([0]) > False > # Ruby > irb(main):001:0> [nil].any? > =3D> false > irb(main):002:0> [false].any? > =3D> false > irb(main):003:0> !!0 > =3D> true > irb(main):004:0> [0].any? > =3D> true > # JavaScript > > [0].some(x=3D>x) > false > > [1].some(x=3D>x) > true > ``` > > It is currently possible to check if code is passing a callable returning= anything other than a boolean > to functions such as `array_filter()` using a wide variety of static anal= yzers/tools, e.g. http://github.com/phan/phan > > ``` > // Phan emits "PhanTypeMismatchArgumentInternal Argument 2 ($callback) is= (fn) of type Closure(int):int > // but \array_filter() takes callable(mixed):bool|callable(mixed,mixed):b= ool" > array_filter([1], fn (int $x): int =3D> $x % 3); > ``` > > Thanks, > -Tyson > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > 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.