Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112900 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 28762 invoked from network); 15 Jan 2021 16:17:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Jan 2021 16:17:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DAEC91804F3 for ; Fri, 15 Jan 2021 07:55:45 -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.2 required=5.0 tests=BAYES_50,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 ; Fri, 15 Jan 2021 07:55:45 -0800 (PST) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id 67D9A5101324; Fri, 15 Jan 2021 15:55:43 +0000 (UTC) To: Larry Garfield , php internals References: Message-ID: Date: Fri, 15 Jan 2021 16:55:42 +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] "TryX" idom for Enumerations From: pierre-php@processus.org ("Pierre R.") Le 10/01/2021 à 22:27, Larry Garfield a écrit : > This is a little tangent from the Enums RFC, but I want to flag it because it it's the sort of in-passing decision that could have far-reaching implications, so shouldn't be done implicitly. > > At the moment, the Enum RFC for scalar enums includes two methods: > > public function has(string $name): bool > > public function from(string $name): self throws ValueError > > Nikita raised the point that has() seems kinda pointless as it would only ever be used to wrap a possibly-unsafe from() call. (In most other cases, calling cases() would be more useful or at least equally useful.) One of the proposed alternatives was the following: > > public function from(string $name): self throws ValueError > > public function tryFrom(string $name): ?self > > The "a method that begins with try is nullable, so watch out" idiom is present in C# and Rust, but to my knowledge has never existed in PHP. That doesn't make it bad; it actually combines quite well with the null coalesce operator to allow for default values, making a valueOrDefault() method unnecessary. > > $order = SortOrder::tryFrom($input) ?? SortOrder::Asc; > > I'm not opposed to following that pattern here; it would allow both a "hard fail" and "soft fail" variant of the operation. However, as noted that idiom has never appeared in PHP before that I'm aware. If we adopt it here, that means it will either start to spread and become a more common PHP idiom over time, OR it won't spread and Enums will have this weird one-off naming convention for a nullable method. The former would, of course, be considerably preferable to the latter. > > So, explicit decision time: Are we OK with introducing that idiom, and then following it consistently in the future in similar situations? (viz, tryX() means nullable, and no-try means not nullable.) I'm good with it if the consensus is good with it, but I want to see what the consensus is first. I'm OK with the tryBar(): ?Foo pattern, but my preference goes over just bar(): ?Foo, as long as API are explicitly typed and correctly documented. I will not fight against or for any or another solution, as long as the convention remains the same for everything. Larry, I think your proposal is missing something: if a convention emerge, it should be documented as such, and be considered as a law for subsequent language / API addition, the real question, IMHO, is : Are people OK with writing naming and design conventions by law in PHP core ? For that matters, I'm OK, this would avoid many bikesheds in the future: this is the law, and the law says, name it tryX(), no more useless votes, no more flavor-oriented flame wars on naming :) -- Pierre