Open JefvdA opened 10 months ago
For getValue
this is a controversial solution. It is not known in which systems this library will be used. Perhaps the developer will expect to get null
on a successful response, which he will want to handle somehow. And we will deprive him of this possibility by throwing him an exception about not being able to get the answer.
@antonowano good point , but you could check if a result is successful or not with the isSuccess()
method. 🤔
I am also thinking over this repo in general , as maybe what I am trying to achieve is not really how you want to write php. I am first going to look into #2 aswell , as it probably will make things clearer aswell.
Also going to use this Result
type in an example project , to get more insight in how it actually could be used.
Thanks for commenting though 😄
I mean the case of $successResult = Result::createFromValue(null);
in the current case, we won't have any problems.
Аfter the task, isSuccess()
will return true
and getValue()
will throw the exception.
Example to understand
$result = Result::createFromValue(null);
if ($result->isSuccess()) { // true
echo $result->getValue(); // throw Exception!
}
This will also affect match()
which should not inherently throw exceptions.
The getters:
getValue
getException
These getters have return types that are nullable (
|null
) but shouldn't be.You can check wether a result is succesful or not with
isSuccess()
so the getter can just throw exceptions instead.