Re: "TryX" idom for Enumerations

From: Date: Tue, 12 Jan 2021 16:13:47 +0000
Subject: Re: "TryX" idom for Enumerations
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-112849@lists.php.net to get a copy of this message
> On Jan 11, 2021, at 19:09, Mike Schinkel <mike@newclarity.net> wrote: > >> On Jan 11, 2021, at 6:52 PM, Larry Garfield <larry@garfieldtech.com> wrote: >> >> On Mon, Jan 11, 2021, at 2:27 PM, Ben Ramsey wrote: >>>> On Jan 10, 2021, at 20:09, Larry Garfield <larry@garfieldtech.com> wrote: >>>> >>>> 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" >>>>>> 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. >> >> >>> I have no problem introducing this idiom (or similar). In fact, I welcome it. >>> >>> In userland, the same concept is often achieved with methods like >>> fromOrNull(), and as Mark points out, the word >>> “try” makes me think >>> the method should throw an exception if it fails. I’m 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. ;-) >>> >>> “There are two hard things in computer science: cache invalidation, >>> naming things, and off-by-one errors.” --Phil Karlton >>> >>> Cheers, >>> Ben >> >> Do you have a suggestion for a better, more PHP-ish naming convention? > > maybeFrom()? I think a maybeX() idiom is better, though it might be confusing to those familiar with functional programming languages that have a Maybe data type. If we had generics, we could introduce a Maybe class, and these methods could return Maybe<self>, for example. That said, maybe ?self is effectively the same thing? For me, maybeX() is great, but I’d like to hear any objections/suggestions from others. Cheers, Ben

Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc
« previous php.internals (#112849) next »