JefvdA / php-result-monad

The PHP Result Monad library provides a simple and effective way to handle the results of actions, offering an alternative approach to exception handling without the need for throwing exceptions.
MIT License
0 stars 0 forks source link

Getters shouldn't be nullable #3

Open JefvdA opened 10 months ago

JefvdA commented 10 months ago

The getters:

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.

antonowano commented 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.

JefvdA commented 10 months ago

@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 😄

antonowano commented 10 months ago

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.