On Tue, Jan 12, 2021, at 10:13 AM, Ben Ramsey wrote: > > On Jan 11, 2021, at 19:09, Mike Schinkel <m...@newclarity.net> wrote: > > > >> On Jan 11, 2021, at 6:52 PM, Larry Garfield <la...@garfieldtech.com> wrote: > >> > >> On Mon, Jan 11, 2021, at 2:27 PM, Ben Ramsey wrote: > >>>> On Jan 10, 2021, at 20:09, Larry Garfield <la...@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.
The potential confusion with a Maybe monad type is my primary concern with that option, especially since being able to define a Maybe type is part 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 on 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-egg problems. My other issue with maybe is that it's a bit long in comparison, but that's somewhat secondary. "Maybe" has enough prior art that it would be even more confusing than "try," I feel. --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php