Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112587 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 59853 invoked from network); 22 Dec 2020 08:05:04 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Dec 2020 08:05:04 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AF00A1804E1 for ; Mon, 21 Dec 2020 23:37:42 -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.5 required=5.0 tests=BAYES_00,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from processus.org (ns366368.ip-94-23-14.eu [94.23.14.201]) (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, 21 Dec 2020 23:37:41 -0800 (PST) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id 8177A5101324; Tue, 22 Dec 2020 07:37:39 +0000 (UTC) To: tyson andre , Sara Golemon Cc: "internals@lists.php.net" References: Message-ID: <477208c5-331d-671e-a8f4-199a751f0a04@processus.org> Date: Tue, 22 Dec 2020 08:37:37 +0100 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Authentication-Results: processus.org; auth=pass smtp.auth=pierre-php@processus.org smtp.mailfrom=pierre-php@processus.org X-Spamd-Bar: / Subject: Re: [PHP-DEV] Straw poll: Naming for `*any()` and `*all()` on iterables From: pierre-php@processus.org (Pierre) Le 22/12/2020 à 01:48, tyson andre a écrit : > Hi Sara Golemon, > >> This is probably going to be a terrible idea because it involves a little magic but... >> >> "iterable" is already a forbidden userspace class name since it's reserved for the psuedo-type which is a union of `Traversable | array`. >> >> What if we went ahead and defined an actual (abstract-final) `iterable` class.  The parser rules which treat `iterable` as a special type still apply, so we never try to match instances of class `iterable`, but what we can do is define methods on it. >> >> class Iterable { >>     static public function any(iterable $iter, Callable $predicate): bool { ... } >>     static public function all(iterable $iter, Callable $predicate): bool { ... } >> } >> >> Then the API is both 100% searchable (the man page for iterable can explain that it's a pseudo-type AND that it has helper methods), and intuitive to read/write. >> >> if (iterable::any($iter, fn($elem) => $elem === 'foo')) { >> >> } > My objections to that are: > > 1. As mentioned in https://wiki.php.net/rfc/any_all_on_iterable_straw_poll#rejected_choices , I'd objected to static methods in general because it would be inconvenient to polyfill > (magic such as __callStatic or disable_classes might be possible but inconvenient and integrate poorly with IDEs/analyzers) > > (if more than 2/3rs of php developers are in favor of more comprehensive functionality later on, such as none(), first(), etc.) > 2. The name `iterable::any` would make it impossible to polyfill iterable::any()/all() in php 8.0 without using PECLS that modify the internals of php in unsafe ways (class_alias also forbids 'iterable' as an alias). I'd still prefer IterUtils/IterableUtils:: over iterable::. > 3. For these reasons, I assume static methods have been fairly unpopular on functionality not associated with an object instance, but the main reason may be something else. > > Also, on an off-topic note, `abstract final` is forbidden for userland classes, so making it have those flags would be surprising for uses of Reflection such as code stub generators, > but forbidding the constructor would be close enough. > Although I think `abstract final` should be allowed like it is in java, > I think RFCs that proposed allowing `abstract final` failed by a large margin years ago and I don't plan to reopen that. > > Thanks, > - Tyson Hello, This is the kind of reasoning I fully support usually, I do maintain a few libraries for multiple PHP versions as most people in this list and make things easily polifyllable is something I'm sensible to. Nevertheless, the language and its standard library has to evolve at some point. For enumerable objects PHP terribly lacks a complete and convenient API for developers. By putting those methods in the global namespace, you make those methods usage much less fluent for many developers that uses a decent IDE. It'd be a great thing to have a compete OO-based (interface based) API for collection methods. any() and all() methods are only the start, in my opinion, of much greater improvements in that regard, and I'd very much love to have those autocompleted by IDE on pretty much everything that is iterable. That's just an opinion, I'd love to see it evolve towards an object-oriented API. Regards, Pierre