Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112852 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 77656 invoked from network); 12 Jan 2021 16:57:32 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Jan 2021 16:57:32 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A7A111804D1 for ; Tue, 12 Jan 2021 08:35:29 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (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 ; Tue, 12 Jan 2021 08:35:29 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id BD89E5C067A for ; Tue, 12 Jan 2021 11:35:27 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute4.internal (MEProxy); Tue, 12 Jan 2021 11:35:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=WSc3IXe2WrZuw8kPCPNIU959Yhrcte66V2U/PQ7uT VM=; b=rBbcy9Bpy3Ll8pFD67XOPL9PQzYwIY3p/YTNJIxvs2Qk6nz3ztsX2OIfN pFmwuiNXTUEhp6bgMXiQfd4DMgr2C62fwyFvc402wbJ+6sbR1ntBxbaNUPK3YKd2 4XN8BisvJ/RG+/Cef2TwRNphl2SvTYxDMb/DrvGZctcpFMh5Jv+vazkXNklSJUVj IeCQ+WX5o77lZbz7C/Pv8sVmoVBsG9PIgtlR9wzo/XT95B92DTfXhaUPIcaFICGq 98a/3n8wbliygKmkrrgyjHZoxcppEG2dSG8iTCqc/XdJrwvWF6LMN8ZO1aNVKdrG d5mDvMG6YFZbzS/uj7j+07u/NSE6A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedukedrtddtgdehkecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtgfesthhqredtreerjeenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeffffffjeffudfggeevvdeitdetvdfgjefffeffjeel feejteevheeghffhvdfgleenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 3EE8614200A2; Tue, 12 Jan 2021 11:35:27 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-45-g4839256-fm-20210104.001-g48392560 Mime-Version: 1.0 Message-ID: <5a75eeed-7034-4771-be28-36323e845157@www.fastmail.com> In-Reply-To: <1183F48B-57BB-4CBE-8474-CF1D7DFD7F6E@benramsey.com> References: <490416F4-7E2F-454F-A317-43212CA6C2D1@benramsey.com> <28f44744-4dc5-4cb8-8751-6b66386119fa@www.fastmail.com> <26074258-B6B5-40EA-A043-E3FAD1FB5365@newclarity.net> <1183F48B-57BB-4CBE-8474-CF1D7DFD7F6E@benramsey.com> Date: Tue, 12 Jan 2021 10:35:06 -0600 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] "TryX" idom for Enumerations From: larry@garfieldtech.com ("Larry Garfield") On Tue, Jan 12, 2021, at 10:13 AM, Ben Ramsey wrote: > > On Jan 11, 2021, at 19:09, Mike Schinkel wrote= : > >=20 > >> On Jan 11, 2021, at 6:52 PM, Larry Garfield wrote: > >>=20 > >> On Mon, Jan 11, 2021, at 2:27 PM, Ben Ramsey wrote: > >>>> On Jan 10, 2021, at 20:09, Larry Garfield wrote: > >>>>=20 > >>>> On Sun, Jan 10, 2021, at 4:40 PM, Mark Randall wrote: > >>>>> On 10/01/2021 21:27, Larry Garfield wrote: > >>>>>> The "a method that begins with try is nullable, so watch out" i= diom 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 th= e null coalesce operator to allow for default values, making a valueOrDe= fault() method unnecessary. > >>=20 > >>=20 > >>> I have no problem introducing this idiom (or similar). In fact, I = welcome it. > >>>=20 > >>> In userland, the same concept is often achieved with methods like > >>> `fromOrNull()`, and as Mark points out, the word =E2=80=9Ctry=E2=80= =9D makes me think > >>> the method should throw an exception if it fails. I=E2=80=99m not = advocating > >>> for `xOrNull()`, though, since I think that smacks of Hungarian > >>> notation, but maybe we can come up with something that is a more > >>> PHP-ish name. ;-) > >>>=20 > >>> =E2=80=9CThere are two hard things in computer science: cache inva= lidation, > >>> naming things, and off-by-one errors.=E2=80=9D --Phil Karlton > >>>=20 > >>> Cheers, > >>> Ben > >>=20 > >> Do you have a suggestion for a better, more PHP-ish naming conventi= on? > >=20 > > maybeFrom()? >=20 >=20 > I think a `maybeX()` idiom is better, though it might be confusing to=20= > those familiar with functional programming languages that have a=20 > `Maybe` data type. >=20 > If we had generics, we could introduce a `Maybe` class, and these=20 > methods could return `Maybe`, for example. That said, maybe=20 > `?self` is effectively the same thing? >=20 > For me, `maybeX()` is great, but I=E2=80=99d like to hear any=20 > objections/suggestions from others. The potential confusion with a Maybe monad type is my primary concern wi= th that option, especially since being able to define a Maybe type is pa= rt of what Ilija and I are working toward with Enums and then ADTs. :-) = (Like, literally, the potential for that is how he got me to join him o= n this project...) : ?self is not quite a Maybe type; it's somewhat similar in net effect, = but a Maybe type forces the caller to then check to see if it's null and= unwrap it. A nullable return will happily work fine without any error = handling... until it fatally doesn't. A Maybe type is essentially what = Swift does in this case. We can't do the same here due to chicken-or-eg= g problems. My other issue with maybe is that it's a bit long in comparison, but tha= t's somewhat secondary. "Maybe" has enough prior art that it would be e= ven more confusing than "try," I feel. --Larry Garfield