Re: [RFC][Discussion] Objects can be declared falsifiable

From: Date: Wed, 02 Nov 2022 08:12:19 +0000
Subject: Re: [RFC][Discussion] Objects can be declared falsifiable
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-118938@lists.php.net to get a copy of this message
Hi Josh, pon., 31 paź 2022 o 20:38 Josh Bruce <josh@joshbruce.dev> napisał(a): > Hello Interntals, > > Someone reached out to me outside of internals regarding the RFC I > submitted on being able to declare objects falsifiable, so, I decided to > update and re-enter the fray. > > I’ve updated the references section as many of the RFCs that were under > discussion at the time have since been implemented. > > I still find myself in situations where having the capability would be > beneficial. Specifically, I’m noticing with API responses where I want to > pass the body received from a ResponseInterface the object can check > itself. I currently use methods such as: > > ->isValid() > ->isInvalid() > ->toBool() > > And similar - the isValid and isInvalid() methods are just aliases of > toBool(), which speaks to the ability for adopters to make their interfaces > compatible without breaking changes in their code. > > In the case of a conditional - looks like this: > > $obj = Object::init($response); > If ($obj->isValid()) { > // do something with $obj > } > > Or: > > If ( > $obj = Object::init($response) and > $obj->isValid() > ) { > // do something with $obj > } > > Would like to be able to do something like: > > If ($obj = Object::init($response)) { > // do something with $obj > } > > As of today, the last example is always true. You wouldn’t be able to > create a guard clause for the negation: > > If ( > $obj = Object::init($response) and > $obj === false > ) { > // handle invalid object, which could include something like > $obj->failed() > } > Have you tried not initializing invalid objects? This could help you. Also that sounds more logical to me as I don't see any reasons to initialize invalid objects if it's a matter of simple validation instead. P.S. I don't see it feasible to have objects that evaluate false in logical expressions. Cheers, Michał Marcin Brzuchalski

« previous php.internals (#118938) next »